From afe0345c5badf17d13c1ef0f7e8b52da3e5b75f6 Mon Sep 17 00:00:00 2001 From: James Hargreaves Date: Sun, 6 Aug 2023 14:37:20 +0000 Subject: [PATCH 1/5] fix and test --- ChangeLog.md | 1 + .../Analysis/UseConstantInsteadOfFieldAnalysis.cs | 13 +++++++++++++ .../RCS1187UseConstantInsteadOfFieldTests.cs | 13 +++++++++++++ 3 files changed, 27 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 1b69e018e2..df449391cf 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Append `?` to nullable reference types. - Fix [RCS1179](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1179.md) ([#1129](https://github.com/JosefPihrt/Roslynator/pull/1129)). - Fix [RCS0060](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS0060.md) ([#1139](https://github.com/JosefPihrt/Roslynator/pull/1139)). +- Fix [RCS1187](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1187.md) ([#1150](https://github.com/JosefPihrt/Roslynator/pull/1150)). ## [4.3.0] - 2023-04-24 diff --git a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs index 8ac531f525..bc492ec1b7 100644 --- a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs +++ b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics; +using System.Linq; using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -82,6 +83,18 @@ 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 (var identifierName in value.DescendantNodesAndSelf().OfType()) + { + 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; +} "); } } From d5237219db2dc40c59f1e460709f30de42e4d73a Mon Sep 17 00:00:00 2001 From: James Hargreaves Date: Sun, 6 Aug 2023 16:15:44 +0100 Subject: [PATCH 2/5] Update UseConstantInsteadOfFieldAnalysis.cs --- src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs index bc492ec1b7..71b37e2fc1 100644 --- a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs +++ b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs @@ -85,7 +85,7 @@ public static bool IsFixable( return false; // Changing a static field to a constant changes the meaning of that symbol in the decorators. - foreach (var identifierName in value.DescendantNodesAndSelf().OfType()) + foreach (IdentifierNameSyntax identifierName in value.DescendantNodesAndSelf().OfType()) { if (identifierName.Identifier.ValueText != fieldSymbol.Name) continue; From 9d26d43b2cff89f1bd11fa9e70ab8afe4ed77b8f Mon Sep 17 00:00:00 2001 From: James Date: Wed, 9 Aug 2023 18:02:32 +0000 Subject: [PATCH 3/5] CR --- .../CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs index 71b37e2fc1..536572bea5 100644 --- a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs +++ b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs @@ -85,8 +85,11 @@ public static bool IsFixable( return false; // Changing a static field to a constant changes the meaning of that symbol in the decorators. - foreach (IdentifierNameSyntax identifierName in value.DescendantNodesAndSelf().OfType()) + foreach (SyntaxNode node in value.DescendantNodesAndSelf()) { + if (node is not IdentifierNameSyntax identifierName) + continue; + if (identifierName.Identifier.ValueText != fieldSymbol.Name) continue; From dbaa8b2c0d7248b1742233154a4c7b325fbb549c Mon Sep 17 00:00:00 2001 From: James Hargreaves Date: Thu, 10 Aug 2023 20:48:24 +0100 Subject: [PATCH 4/5] Update ChangeLog.md --- ChangeLog.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index ed7a41a014..20d24c135d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add SECURITY.md ([#1147](https://github.com/josefpihrt/roslynator/pull/1147)) +### Fixed + +- Fix [RCS1187](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1187.md) ([#1150](https://github.com/JosefPihrt/Roslynator/pull/1150)). + ## [4.4.0] - 2023-08-01 ### Added @@ -45,7 +49,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Append `?` to nullable reference types. - Fix [RCS1179](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1179.md) ([#1129](https://github.com/JosefPihrt/Roslynator/pull/1129)). - Fix [RCS0060](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS0060.md) ([#1139](https://github.com/JosefPihrt/Roslynator/pull/1139)). -- Fix [RCS1187](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1187.md) ([#1150](https://github.com/JosefPihrt/Roslynator/pull/1150)). ## [4.3.0] - 2023-04-24 From 275f182b15cbb3b504638442ba2c0888ce66a13b Mon Sep 17 00:00:00 2001 From: James Hargreaves Date: Fri, 11 Aug 2023 19:37:00 +0100 Subject: [PATCH 5/5] Update UseConstantInsteadOfFieldAnalysis.cs --- src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs index 536572bea5..1cc84e749a 100644 --- a/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs +++ b/src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs @@ -2,7 +2,6 @@ using System; using System.Diagnostics; -using System.Linq; using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp;