Skip to content

Commit

Permalink
Fix RCS1187 (#1150)
Browse files Browse the repository at this point in the history
Co-authored-by: Josef Pihrt <josef@pihrt.net>
  • Loading branch information
jamesHargreaves12 and josefpihrt committed Aug 12, 2023
1 parent b58e8d0 commit 25288a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ internal static class UseConstantInsteadOfFieldAnalysis

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)
Expand Down
13 changes: 13 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1187UseConstantInsteadOfFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
");
}
}

0 comments on commit 25288a7

Please sign in to comment.