|
| 1 | +using Cslint.Core.Config; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | + |
| 6 | +namespace Cslint.Core.Rules.Tier3; |
| 7 | + |
| 8 | +public sealed class SimplePropertyAccessorsRule : IRuleDefinition, IDescendantNodeHandler |
| 9 | +{ |
| 10 | + public string RuleId => "IDE0360"; |
| 11 | + |
| 12 | + public string Name => "SimplePropertyAccessors"; |
| 13 | + |
| 14 | + public IReadOnlyList<string> ConfigKeys { get; } = ["csharp_style_prefer_simple_property_accessors"]; |
| 15 | + |
| 16 | + public LintSeverity DefaultSeverity => LintSeverity.Info; |
| 17 | + |
| 18 | + private static string? TryGetSimpleGetField(AccessorDeclarationSyntax getter) |
| 19 | + { |
| 20 | + // Expression body: get => _field; |
| 21 | + if (getter.ExpressionBody is { Expression: IdentifierNameSyntax exprId }) |
| 22 | + { |
| 23 | + return exprId.Identifier.Text; |
| 24 | + } |
| 25 | + |
| 26 | + // Block body: get { return _field; } |
| 27 | + if (getter.Body is { Statements: [ReturnStatementSyntax { Expression: IdentifierNameSyntax returnId } ] }) |
| 28 | + { |
| 29 | + return returnId.Identifier.Text; |
| 30 | + } |
| 31 | + |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + private static string? TryGetSimpleSetField(AccessorDeclarationSyntax setter) |
| 36 | + { |
| 37 | + // Expression body: set => _field = value; |
| 38 | + if (setter.ExpressionBody?.Expression is AssignmentExpressionSyntax exprAssign && |
| 39 | + exprAssign.IsKind(SyntaxKind.SimpleAssignmentExpression) && |
| 40 | + exprAssign.Left is IdentifierNameSyntax exprLeft && |
| 41 | + exprAssign.Right is IdentifierNameSyntax exprRight && |
| 42 | + exprRight.Identifier.Text == "value") |
| 43 | + { |
| 44 | + return exprLeft.Identifier.Text; |
| 45 | + } |
| 46 | + |
| 47 | + // Block body: set { _field = value; } |
| 48 | + if (setter.Body is |
| 49 | + { |
| 50 | + Statements: |
| 51 | + [ |
| 52 | + ExpressionStatementSyntax |
| 53 | + { |
| 54 | + Expression: AssignmentExpressionSyntax blockAssign, |
| 55 | + }, |
| 56 | + ], |
| 57 | + } && |
| 58 | + blockAssign.IsKind(SyntaxKind.SimpleAssignmentExpression) && |
| 59 | + blockAssign.Left is IdentifierNameSyntax blockLeft && |
| 60 | + blockAssign.Right is IdentifierNameSyntax blockRight && |
| 61 | + blockRight.Identifier.Text == "value") |
| 62 | + { |
| 63 | + return blockLeft.Identifier.Text; |
| 64 | + } |
| 65 | + |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + public bool IsEnabled(LintConfiguration configuration) => |
| 70 | + configuration.GetValue("csharp_style_prefer_simple_property_accessors") is not null; |
| 71 | + |
| 72 | + public IReadOnlyList<LintDiagnostic> Analyze(RuleContext context) |
| 73 | + { |
| 74 | + (string? pref, string? _) = context.Configuration |
| 75 | + .GetValueWithSeverity("csharp_style_prefer_simple_property_accessors"); |
| 76 | + |
| 77 | + if (!string.Equals(pref, "true", StringComparison.OrdinalIgnoreCase)) |
| 78 | + { |
| 79 | + return []; |
| 80 | + } |
| 81 | + |
| 82 | + var diagnostics = new List<LintDiagnostic>(); |
| 83 | + |
| 84 | + foreach (SyntaxNode node in context.Root.DescendantNodes()) |
| 85 | + { |
| 86 | + VisitNode(node, context.Configuration, context.FilePath, diagnostics); |
| 87 | + } |
| 88 | + |
| 89 | + return diagnostics; |
| 90 | + } |
| 91 | + |
| 92 | + public void VisitNode( |
| 93 | + SyntaxNode node, |
| 94 | + LintConfiguration config, |
| 95 | + string filePath, |
| 96 | + List<LintDiagnostic> diagnostics) |
| 97 | + { |
| 98 | + if (node is not PropertyDeclarationSyntax property) |
| 99 | + { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + if (property.AccessorList is null) |
| 104 | + { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + (string? pref, string? _) = config.GetValueWithSeverity("csharp_style_prefer_simple_property_accessors"); |
| 109 | + |
| 110 | + if (!string.Equals(pref, "true", StringComparison.OrdinalIgnoreCase)) |
| 111 | + { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + AccessorDeclarationSyntax? getter = null; |
| 116 | + AccessorDeclarationSyntax? setter = null; |
| 117 | + |
| 118 | + foreach (AccessorDeclarationSyntax accessor in property.AccessorList.Accessors) |
| 119 | + { |
| 120 | + if (accessor.IsKind(SyntaxKind.GetAccessorDeclaration)) |
| 121 | + { |
| 122 | + getter = accessor; |
| 123 | + } |
| 124 | + else if (accessor.IsKind(SyntaxKind.SetAccessorDeclaration) || |
| 125 | + accessor.IsKind(SyntaxKind.InitAccessorDeclaration)) |
| 126 | + { |
| 127 | + setter = accessor; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Need both get and set/init |
| 132 | + if (getter is null || setter is null) |
| 133 | + { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // Both must be non-auto (have a body or expression body) |
| 138 | + if (getter.Body is null && getter.ExpressionBody is null) |
| 139 | + { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + if (setter.Body is null && setter.ExpressionBody is null) |
| 144 | + { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + string? getField = TryGetSimpleGetField(getter); |
| 149 | + string? setField = TryGetSimpleSetField(setter); |
| 150 | + |
| 151 | + // Both must be simple forwards to the same field |
| 152 | + if (getField is null || setField is null || getField != setField) |
| 153 | + { |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + FileLinePositionSpan span = property.Identifier.GetLocation().GetLineSpan(); |
| 158 | + |
| 159 | + diagnostics.Add( |
| 160 | + new LintDiagnostic |
| 161 | + { |
| 162 | + RuleId = RuleId, |
| 163 | + Message = $"Property '{property.Identifier.Text}' can be an auto-property (backing field '{getField}')", |
| 164 | + Severity = LintSeverity.Info, |
| 165 | + FilePath = filePath, |
| 166 | + Line = span.StartLinePosition.Line + 1, |
| 167 | + Column = span.StartLinePosition.Character + 1, |
| 168 | + }); |
| 169 | + } |
| 170 | +} |
0 commit comments