Skip to content

Commit d97d40b

Browse files
authored
Merge pull request #59 from samcragg/master
Check for no SymbolInfo for variables
2 parents d080069 + 5f8adfe commit d97d40b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ public static void Main() { }
9191
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
9292
}
9393

94+
[TestMethod]
95+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/58")]
96+
public void StaticWithNameof_ShouldNotThrow()
97+
{
98+
const string source = @"public class TestClass
99+
{
100+
private static string StaticResult { get; set; }
101+
102+
public static void Main()
103+
{
104+
StaticResult = nameof(Main);
105+
}
106+
}";
107+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
108+
}
109+
94110
[TestMethod]
95111
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/49")]
96112
public void WritingToConsole_ShouldNotThrow()

src/FluentAssertions.Analyzers/Utilities/VariableNameExtractor.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ public static string ExtractVariabeName(InvocationExpressionSyntax invocation)
5353

5454
private bool IsVariable(IdentifierNameSyntax node)
5555
{
56-
// TODO: cleanup
57-
if (_semanticModel == null) return true;
56+
if (_semanticModel != null)
57+
{
58+
SymbolInfo info = _semanticModel.GetSymbolInfo(node);
59+
if (info.Symbol == null ||
60+
info.Symbol.Kind == SymbolKind.Method ||
61+
info.Symbol.IsStatic)
62+
{
63+
return false;
64+
}
65+
}
5866

59-
var info = _semanticModel.GetSymbolInfo(node);
60-
if (info.Symbol.Kind == SymbolKind.Method || info.Symbol.IsStatic) return false;
6167
return true;
6268
}
6369
}

0 commit comments

Comments
 (0)