Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Fix dictionary custom methods false diagnostics #379

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/DictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ public class DictionaryTests
[Implemented]
public void DictionaryShouldContainKey_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.DictionaryShouldContainKey_ContainsKeyShouldBeTrue);

[DataTestMethod]
[DataRow(
@"using System.Collections.Generic;
using FluentAssertions;

namespace TestNamespace
{
public class MultiKeyDict<TKey1, TKey2, TValue> : Dictionary<TKey1, Dictionary<TKey2, TValue>>
{
public bool ContainsKey(TKey1 key1, TKey2 key2) => false;
public bool ContainsValue(TKey1 key1, TKey2 key2) => false;
}

public class TestClass
{
public void TestMethod(MultiKeyDict<int, int, string> actual)
{
actual.ContainsKey(0, 1).Should().BeTrue();
actual.ContainsValue(0, 1).Should().BeTrue();
}
}
}")]
[Implemented]
public void DictionaryMethods_CustomMethods_TestNoAnalyzer(string code) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(code);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.ContainsKey(expectedKey).Should().BeTrue({0});",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public const string DiagnosticId = "FAA0001";
public const string Message = "Clean up FluentAssertion usage";

protected static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, Message, Constants.Tips.Category, DiagnosticSeverity.Info, true);

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

Expand Down Expand Up @@ -49,6 +49,7 @@
return;
}

context.Options.AnalyzerConfigOptionsProvider.GetOptions(invocation.Syntax.SyntaxTree).TryGetValue("use_diagnostic_per_assertion", out var useDiagnosticPerAssertion);
if (HasConditionalAccessAncestor(invocation))
{
var expressionStatement = invocation.GetFirstAncestor<IExpressionStatementOperation>();
Expand Down Expand Up @@ -211,10 +212,12 @@
case nameof(string.StartsWith) when invocationBeforeShould.IsContainedInType(SpecialType.System_String):
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.StringShouldStartWith_StartsWithShouldBeTrue));
return;
case nameof(IDictionary<string, object>.ContainsKey) when invocationBeforeShould.ImplementsOrIsInterface(metadata.IDictionaryOfT2) || invocationBeforeShould.ImplementsOrIsInterface(metadata.IReadonlyDictionaryOfT2):
case nameof(Dictionary<string, object>.ContainsKey) when (invocationBeforeShould.ImplementsOrIsInterface(metadata.IDictionaryOfT2) || invocationBeforeShould.ImplementsOrIsInterface(metadata.IReadonlyDictionaryOfT2))
&& invocationBeforeShould.AreMethodParameterSameTypeAsContainingTypeArguments((parameter: 0, typeArgument: 0)):
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.DictionaryShouldContainKey_ContainsKeyShouldBeTrue));
return;
case nameof(Dictionary<string, object>.ContainsValue) when invocationBeforeShould.IsContainedInType(metadata.DictionaryOfT2):
case nameof(Dictionary<string, object>.ContainsValue) when invocationBeforeShould.IsContainedInType(metadata.DictionaryOfT2)
&& invocationBeforeShould.AreMethodParameterSameTypeAsContainingTypeArguments((parameter: 0, typeArgument: 1)):
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.DictionaryShouldContainValue_ContainsValueShouldBeTrue));
return;
}
Expand Down
17 changes: 17 additions & 0 deletions src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ public static bool IsTypeof(this IArgumentOperation argument, SpecialType type)
public static bool IsTypeof(this IArgumentOperation argument, INamedTypeSymbol type)
=> argument.Value.UnwrapConversion().Type.EqualsSymbol(type);

public static bool AreMethodParameterSameTypeAsContainingTypeArguments(this IInvocationOperation invocation, params (int parameter, int typeArgument)[] parameters)
{
if (invocation.TargetMethod.Parameters.Length != parameters.Length)
return false;

if (invocation.TargetMethod.ContainingType.TypeArguments.Length < parameters.Max(x => x.typeArgument))
return false;

foreach (var (parameter, typeArgument) in parameters)
{
if (!invocation.TargetMethod.Parameters[parameter].Type.EqualsSymbol(invocation.TargetMethod.ContainingType.TypeArguments[typeArgument]))
return false;
}

return true;
}

public static bool IsSameArgumentReference(this IArgumentOperation argument1, IArgumentOperation argument2)
{
return argument1.TryGetFirstDescendent<IParameterReferenceOperation>(out var argument1Reference)
Expand Down
Loading