Skip to content

Commit 44e2860

Browse files
ErikSchierboomMeir017
authored andcommitted
Fix null-reference exception when writing to console (#48)
1 parent 01a24c0 commit 44e2860

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/FluentAssertions.Analyzers.Tests/DiagnosticVerifier.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static DiagnosticVerifier()
2828
References = new[]
2929
{
3030
typeof(object), // System.Private.CoreLib
31+
typeof(Console), // System
3132
typeof(Enumerable), // System.Linq
3233
typeof(CSharpCompilation), // Microsoft.CodeAnalysis.CSharp
3334
typeof(Compilation), // Microsoft.CodeAnalysis

src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,21 @@ public static void Main() { }
9090

9191
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
9292
}
93+
94+
[TestMethod]
95+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/49")]
96+
public void WritingToConsole_ShouldNotThrow()
97+
{
98+
const string source = @"
99+
public class TestClass
100+
{
101+
public static void Main()
102+
{
103+
System.Console.WriteLine();
104+
}
105+
}";
106+
107+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
108+
}
93109
}
94110
}

src/FluentAssertions.Analyzers/Utilities/FluentAssertionsAnalyzer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected virtual Diagnostic AnalyzeExpression(ExpressionSyntax expression, Sema
5858
var variableNameExtractor = new VariableNameExtractor(semanticModel);
5959
expression.Accept(variableNameExtractor);
6060

61+
if (variableNameExtractor.VariableIdentifierName == null) return null;
6162
var typeInfo = semanticModel.GetTypeInfo(variableNameExtractor.VariableIdentifierName);
6263
if (typeInfo.ConvertedType == null) return null;
6364
if (!ShouldAnalyzeVariableType(typeInfo.ConvertedType)) return null;

0 commit comments

Comments
 (0)