diff --git a/ChangeLog.md b/ChangeLog.md index 5f34868ebb..c5cf396474 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fix [RCS1187](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1187.md) ([#1150](https://github.com/JosefPihrt/Roslynator/pull/1150)). - Fix [RCS1056](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1056.md) ([#1154](https://github.com/JosefPihrt/Roslynator/pull/1154)). ## [4.4.0] - 2023-08-01 diff --git a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs index 8ac531f525..1cc84e749a 100644 --- a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs +++ b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs @@ -82,6 +82,21 @@ public static bool IsFixable( if (!semanticModel.HasConstantValue(value, cancellationToken)) return false; + + // Changing a static field to a constant changes the meaning of that symbol in the decorators. + foreach (SyntaxNode node in value.DescendantNodesAndSelf()) + { + if (node is not IdentifierNameSyntax identifierName) + continue; + + if (identifierName.Identifier.ValueText != fieldSymbol.Name) + continue; + + if (identifierName.Parent is MemberAccessExpressionSyntax memberAccessExpression && memberAccessExpression.Name == identifierName) + continue; + + return false; + } } foreach (IMethodSymbol constructorSymbol in fieldSymbol.ContainingType.StaticConstructors) diff --git a/src/Tests/Analyzers.Tests/RCS1187UseConstantInsteadOfFieldTests.cs b/src/Tests/Analyzers.Tests/RCS1187UseConstantInsteadOfFieldTests.cs index 9313fefc13..8eca38b1d0 100644 --- a/src/Tests/Analyzers.Tests/RCS1187UseConstantInsteadOfFieldTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1187UseConstantInsteadOfFieldTests.cs @@ -86,6 +86,19 @@ static void M(in int value) { } } +"); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseConstantInsteadOfField)] + public async Task TestNoDiagnostic_SelfReference() + { + await VerifyNoDiagnosticAsync(@" +using System; + +class C +{ + private static readonly Double Double = Double.Epsilon; +} "); } }