Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ 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)
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;
}
");
}
}