Skip to content

Commit

Permalink
Fix S2234 Bug: AD0001 is thrown due to referencing a location outside…
Browse files Browse the repository at this point in the history
… of the current compilation (#9323)
  • Loading branch information
pavel-mikula-sonarsource committed May 27, 2024
1 parent fd68d59 commit 5fb5a91
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public abstract class ParametersCorrectOrderBase<TSyntaxKind> : SonarDiagnosticA
&& Language.Syntax.ArgumentList(c.Node).FirstOrDefault(x => MatchingNames(parameterSymbol, ArgumentName(x))) is { })
{
var secondaryLocations = methodParameterLookup.MethodSymbol.DeclaringSyntaxReferences
.Select(s => Language.Syntax.NodeIdentifier(s.GetSyntax())?.GetLocation())
.Select(x => Language.Syntax.NodeIdentifier(x.GetSyntax())?.ToSecondaryLocation())
.WhereNotNull();
c.ReportIssue(Diagnostic.Create(Rule, PrimaryLocation(c.Node), secondaryLocations, properties: null, methodParameterLookup.MethodSymbol.Name));
c.ReportIssue(Rule, PrimaryLocation(c.Node), secondaryLocations, methodParameterLookup.MethodSymbol.Name);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,27 @@ public void Bar()
End Sub
End Class
""").VerifyNoIssuesIgnoreErrors();

[TestMethod]
public async Task ParametersCorrectOrder_SecondaryLocationsOutsideCurrentCompilation()
{
var library = TestHelper.CompileCS("""
public static class Library
{
public static void Method(int a, int b) { }
}
""").Model.Compilation;
var usage = TestHelper.CompileCS("""
public class Usage
{
public void Method()
{
int a = 4, b = 2;
Library.Method(b, a);
}
}
""", library.ToMetadataReference()).Model.Compilation;
var diagnostics = await usage.WithAnalyzers([new CS.ParametersCorrectOrder()]).GetAnalyzerDiagnosticsAsync();
diagnostics.Should().ContainSingle().Which.Id.Should().Be("S2234", "we don't want AD0001 here");
}
}

0 comments on commit 5fb5a91

Please sign in to comment.