-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule MA0143: Primary constructor parameters should be readonly (#652
- Loading branch information
Showing
11 changed files
with
311 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# MA0143 - Primary constructor parameters should be readonly | ||
|
||
The rule reports all assignments to primary constructor parameters | ||
|
||
````c# | ||
class Sample(int p) | ||
{ | ||
void A() | ||
{ | ||
p = 0; // non-compliant | ||
(p, _) = (0, 1); // non-compliant | ||
} | ||
} | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/Meziantou.Analyzer/Rules/PrimaryConstructorParameterShouldBeReadOnlyAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#if CSHARP12_OR_GREATER | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Meziantou.Analyzer.Rules; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class PrimaryConstructorParameterShouldBeReadOnlyAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly DiagnosticDescriptor s_rule = new( | ||
RuleIdentifiers.PrimaryConstructorParameterShouldBeReadOnly, | ||
title: "Primary constructor parameters should be readonly", | ||
messageFormat: "Primary constructor parameters should be readonly", | ||
RuleCategories.Design, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: false, | ||
description: "", | ||
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.PrimaryConstructorParameterShouldBeReadOnly)); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
||
context.RegisterCompilationStartAction(context => | ||
{ | ||
if (context.Compilation.GetCSharpLanguageVersion() < Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp12) | ||
return; | ||
context.RegisterOperationAction(AnalyzerAssignment, OperationKind.SimpleAssignment); | ||
context.RegisterOperationAction(AnalyzerAssignment, OperationKind.CompoundAssignment); | ||
context.RegisterOperationAction(AnalyzerAssignment, OperationKind.CoalesceAssignment); | ||
context.RegisterOperationAction(AnalyzerAssignment, OperationKind.DeconstructionAssignment); | ||
context.RegisterOperationAction(AnalyzerIncrementOrDecrement, OperationKind.Increment); | ||
context.RegisterOperationAction(AnalyzerIncrementOrDecrement, OperationKind.Decrement); | ||
}); | ||
} | ||
|
||
private void AnalyzerIncrementOrDecrement(OperationAnalysisContext context) | ||
{ | ||
var operation = (IIncrementOrDecrementOperation)context.Operation; | ||
var target = operation.Target; | ||
|
||
if (IsPrimaryConstructorParameter(target, context.CancellationToken)) | ||
{ | ||
context.ReportDiagnostic(s_rule, target); | ||
} | ||
} | ||
|
||
private void AnalyzerAssignment(OperationAnalysisContext context) | ||
{ | ||
var operation = (IAssignmentOperation)context.Operation; | ||
var target = operation.Target; | ||
if (target is ITupleOperation) | ||
{ | ||
foreach (var innerTarget in GetAllAssignmentTargets(target)) | ||
{ | ||
if (IsPrimaryConstructorParameter(innerTarget, context.CancellationToken)) | ||
{ | ||
context.ReportDiagnostic(s_rule, innerTarget); | ||
} | ||
} | ||
} | ||
else if (IsPrimaryConstructorParameter(target, context.CancellationToken)) | ||
{ | ||
context.ReportDiagnostic(s_rule, target); | ||
} | ||
} | ||
|
||
private static List<IOperation> GetAllAssignmentTargets(IOperation operation) | ||
{ | ||
var result = new List<IOperation>(); | ||
GetAllAssignmentTargets(result, operation); | ||
return result; | ||
|
||
static void GetAllAssignmentTargets(List<IOperation> operations, IOperation operation) | ||
{ | ||
if (operation is ITupleOperation tuple) | ||
{ | ||
foreach (var element in tuple.Elements) | ||
{ | ||
GetAllAssignmentTargets(operations, element); | ||
} | ||
} | ||
else | ||
{ | ||
operations.Add(operation); | ||
} | ||
} | ||
} | ||
|
||
private static bool IsPrimaryConstructorParameter(IOperation operation, CancellationToken cancellationToken) | ||
{ | ||
if (operation is IParameterReferenceOperation parameterReferenceOperation) | ||
{ | ||
if (parameterReferenceOperation.Parameter.ContainingSymbol is IMethodSymbol { MethodKind: MethodKind.Constructor } ctor) | ||
{ | ||
foreach (var syntaxRef in ctor.DeclaringSyntaxReferences) | ||
{ | ||
var syntax = syntaxRef.GetSyntax(cancellationToken); | ||
if (syntax is ClassDeclarationSyntax or StructDeclarationSyntax) | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.