diff --git a/src/Meziantou.Analyzer.CodeFixers/Internals/ArgumentListHelper.cs b/src/Meziantou.Analyzer.CodeFixers/Internals/ArgumentListHelper.cs index 2d4b25b2a..b0cf407dd 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Internals/ArgumentListHelper.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Internals/ArgumentListHelper.cs @@ -114,10 +114,17 @@ public static InvocationExpressionSyntax WithArguments(InvocationExpressionSynta private sealed class BindingContext(SemanticModel semanticModel, InvocationExpressionSyntax invocationExpression, string? namespaceToImport, CancellationToken cancellationToken) { private readonly bool _isInConditionalAccess = IsInConditionalAccess(invocationExpression.Expression); + + // An attribute argument must be a constant (CS0182), so an invocation cannot be one, and the speculative binding can throw there + private readonly bool _isInAttributeArgument = invocationExpression.FirstAncestorOrSelf() is not null; + private (SemanticModel SemanticModel, int Position)? _bindingLocation; public IMethodSymbol? GetTargetMethod(InvocationExpressionSyntax newInvocation) { + if (_isInAttributeArgument) + return null; + if (_isInConditionalAccess) { var (newSemanticModel, newNode) = ReplaceInvocation(newInvocation); diff --git a/tests/Meziantou.Analyzer.Test/Rules/UseIFormatProviderAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/UseIFormatProviderAnalyzerTests.cs index 94c8baa7f..b74b2deee 100755 --- a/tests/Meziantou.Analyzer.Test/Rules/UseIFormatProviderAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/UseIFormatProviderAnalyzerTests.cs @@ -1283,4 +1283,21 @@ public static class SampleExtensions return test.RunAsync(); } + + [Fact] + [Trait("Issue", "https://github.com/meziantou/Meziantou.Analyzer/issues/1686")] + public Task InvalidCode_InvocationInAttributeArgument_NoCodeFix() + { + const string Source = """ + [assembly:({|MA0011:string.Format(""new object[]{-1a is g System + |}using> + """; + var test = CreateTest(); + // The code does not compile, so the compiler diagnostics cannot be verified + test.CompilerDiagnostics = CompilerDiagnostics.None; + test.TestCode = Source; + test.FixedCode = Source; + + return test.RunAsync(); + } }