Skip to content

Commit 27c9ec1

Browse files
authored
fixed dictionary analyzers analyzing everything (#69)
* fixed dictionary analyzers analyzing everything * cleanup
1 parent 0a478e0 commit 27c9ec1

File tree

9 files changed

+48
-8
lines changed

9 files changed

+48
-8
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,34 @@ public void StringShouldNotBeEmptyAndShouldNotBeNull_ShouldNotTrigger()
160160

161161
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
162162
}
163+
164+
[TestMethod]
165+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/65")]
166+
public void CustomClass_ShouldNotTrigger_DictionaryAnalyzers()
167+
{
168+
const string source = @"
169+
using System.Linq;
170+
using FluentAssertions;
171+
using FluentAssertions.Extensions;
172+
173+
namespace TestNamespace
174+
{
175+
class MyDict<TKey, TValue>
176+
{
177+
public bool ContainsKey(TKey key) => false;
178+
}
179+
180+
public class Program
181+
{
182+
public static void Main()
183+
{
184+
var dict = new MyDict<int, string>();
185+
dict.ContainsKey(0).Should().BeTrue();
186+
}
187+
}
188+
}";
189+
190+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
191+
}
163192
}
164193
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Linq;
2+
using Microsoft.CodeAnalysis;
3+
4+
namespace FluentAssertions.Analyzers
5+
{
6+
public abstract class DictionaryAnalyzer : FluentAssertionsAnalyzer
7+
{
8+
protected override bool ShouldAnalyzeVariableType(ITypeSymbol type)
9+
=> type.AllInterfaces.Any(@interface => @interface.Name == "IDictionary");
10+
}
11+
}

src/FluentAssertions.Analyzers/Tips/Dictionaries/DictionaryShouldContainKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace FluentAssertions.Analyzers
1010
{
1111
[DiagnosticAnalyzer(LanguageNames.CSharp)]
12-
public class DictionaryShouldContainKeyAnalyzer : FluentAssertionsAnalyzer
12+
public class DictionaryShouldContainKeyAnalyzer : DictionaryAnalyzer
1313
{
1414
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainKey;
1515
public const string Category = Constants.Tips.Category;

src/FluentAssertions.Analyzers/Tips/Dictionaries/DictionaryShouldContainKeyAndValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace FluentAssertions.Analyzers
1111
{
1212
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13-
public class DictionaryShouldContainKeyAndValueAnalyzer : FluentAssertionsAnalyzer
13+
public class DictionaryShouldContainKeyAndValueAnalyzer : DictionaryAnalyzer
1414
{
1515
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainKeyAndValue;
1616
public const string Category = Constants.Tips.Category;

src/FluentAssertions.Analyzers/Tips/Dictionaries/DictionaryShouldContainPair.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace FluentAssertions.Analyzers
1111
{
1212
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13-
public class DictionaryShouldContainPairAnalyzer : FluentAssertionsAnalyzer
13+
public class DictionaryShouldContainPairAnalyzer : DictionaryAnalyzer
1414
{
1515
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainPair;
1616
public const string Category = Constants.Tips.Category;

src/FluentAssertions.Analyzers/Tips/Dictionaries/DictionaryShouldContainValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace FluentAssertions.Analyzers
1010
{
1111
[DiagnosticAnalyzer(LanguageNames.CSharp)]
12-
public class DictionaryShouldContainValueAnalyzer : FluentAssertionsAnalyzer
12+
public class DictionaryShouldContainValueAnalyzer : DictionaryAnalyzer
1313
{
1414
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainValue;
1515
public const string Category = Constants.Tips.Category;

src/FluentAssertions.Analyzers/Tips/Dictionaries/DictionaryShouldNotContainKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace FluentAssertions.Analyzers
1010
{
1111
[DiagnosticAnalyzer(LanguageNames.CSharp)]
12-
public class DictionaryShouldNotContainKeyAnalyzer : FluentAssertionsAnalyzer
12+
public class DictionaryShouldNotContainKeyAnalyzer : DictionaryAnalyzer
1313
{
1414
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldNotContainKey;
1515
public const string Category = Constants.Tips.Category;

src/FluentAssertions.Analyzers/Tips/Dictionaries/DictionaryShouldNotContainValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace FluentAssertions.Analyzers
1010
{
1111
[DiagnosticAnalyzer(LanguageNames.CSharp)]
12-
public class DictionaryShouldNotContainValueAnalyzer : FluentAssertionsAnalyzer
12+
public class DictionaryShouldNotContainValueAnalyzer : DictionaryAnalyzer
1313
{
1414
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldNotContainValue;
1515
public const string Category = Constants.Tips.Category;

src/FluentAssertions.Analyzers/Utilities/FluentAssertionsAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ protected virtual Diagnostic AnalyzeExpression(ExpressionSyntax expression, Sema
6060

6161
if (variableNameExtractor.VariableIdentifierName == null) return null;
6262
var typeInfo = semanticModel.GetTypeInfo(variableNameExtractor.VariableIdentifierName);
63-
if (typeInfo.ConvertedType == null) return null;
64-
if (!ShouldAnalyzeVariableType(typeInfo.ConvertedType)) return null;
63+
if (typeInfo.Type == null) return null;
64+
if (!ShouldAnalyzeVariableType(typeInfo.Type)) return null;
6565

6666
foreach (var visitor in Visitors)
6767
{

0 commit comments

Comments
 (0)