Skip to content

Commit

Permalink
fixed dictionary analyzers analyzing everything (#69)
Browse files Browse the repository at this point in the history
* fixed dictionary analyzers analyzing everything

* cleanup
  • Loading branch information
Meir017 authored Jul 5, 2018
1 parent 0a478e0 commit 27c9ec1
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 8 deletions.
29 changes: 29 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,34 @@ public void StringShouldNotBeEmptyAndShouldNotBeNull_ShouldNotTrigger()

DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
}

[TestMethod]
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/65")]
public void CustomClass_ShouldNotTrigger_DictionaryAnalyzers()
{
const string source = @"
using System.Linq;
using FluentAssertions;
using FluentAssertions.Extensions;
namespace TestNamespace
{
class MyDict<TKey, TValue>
{
public bool ContainsKey(TKey key) => false;
}
public class Program
{
public static void Main()
{
var dict = new MyDict<int, string>();
dict.ContainsKey(0).Should().BeTrue();
}
}
}";

DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Linq;
using Microsoft.CodeAnalysis;

namespace FluentAssertions.Analyzers
{
public abstract class DictionaryAnalyzer : FluentAssertionsAnalyzer
{
protected override bool ShouldAnalyzeVariableType(ITypeSymbol type)
=> type.AllInterfaces.Any(@interface => @interface.Name == "IDictionary");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace FluentAssertions.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class DictionaryShouldContainKeyAnalyzer : FluentAssertionsAnalyzer
public class DictionaryShouldContainKeyAnalyzer : DictionaryAnalyzer
{
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainKey;
public const string Category = Constants.Tips.Category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace FluentAssertions.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class DictionaryShouldContainKeyAndValueAnalyzer : FluentAssertionsAnalyzer
public class DictionaryShouldContainKeyAndValueAnalyzer : DictionaryAnalyzer
{
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainKeyAndValue;
public const string Category = Constants.Tips.Category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace FluentAssertions.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class DictionaryShouldContainPairAnalyzer : FluentAssertionsAnalyzer
public class DictionaryShouldContainPairAnalyzer : DictionaryAnalyzer
{
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainPair;
public const string Category = Constants.Tips.Category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace FluentAssertions.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class DictionaryShouldContainValueAnalyzer : FluentAssertionsAnalyzer
public class DictionaryShouldContainValueAnalyzer : DictionaryAnalyzer
{
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainValue;
public const string Category = Constants.Tips.Category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace FluentAssertions.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class DictionaryShouldNotContainKeyAnalyzer : FluentAssertionsAnalyzer
public class DictionaryShouldNotContainKeyAnalyzer : DictionaryAnalyzer
{
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldNotContainKey;
public const string Category = Constants.Tips.Category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace FluentAssertions.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class DictionaryShouldNotContainValueAnalyzer : FluentAssertionsAnalyzer
public class DictionaryShouldNotContainValueAnalyzer : DictionaryAnalyzer
{
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldNotContainValue;
public const string Category = Constants.Tips.Category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ protected virtual Diagnostic AnalyzeExpression(ExpressionSyntax expression, Sema

if (variableNameExtractor.VariableIdentifierName == null) return null;
var typeInfo = semanticModel.GetTypeInfo(variableNameExtractor.VariableIdentifierName);
if (typeInfo.ConvertedType == null) return null;
if (!ShouldAnalyzeVariableType(typeInfo.ConvertedType)) return null;
if (typeInfo.Type == null) return null;
if (!ShouldAnalyzeVariableType(typeInfo.Type)) return null;

foreach (var visitor in Visitors)
{
Expand Down

0 comments on commit 27c9ec1

Please sign in to comment.