|
| 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 SystemThreadingLockRule : IRuleDefinition, IDescendantNodeHandler |
| 9 | +{ |
| 10 | + public string RuleId => "IDE0330"; |
| 11 | + |
| 12 | + public string Name => "SystemThreadingLock"; |
| 13 | + |
| 14 | + public IReadOnlyList<string> ConfigKeys { get; } = ["csharp_prefer_system_threading_lock"]; |
| 15 | + |
| 16 | + public LintSeverity DefaultSeverity => LintSeverity.Info; |
| 17 | + |
| 18 | + private static bool IsObjectType(TypeSyntax type) => |
| 19 | + type is PredefinedTypeSyntax predefined && predefined.Keyword.IsKind(SyntaxKind.ObjectKeyword); |
| 20 | + |
| 21 | + private static TypeDeclarationSyntax? FindEnclosingType(SyntaxNode node) |
| 22 | + { |
| 23 | + SyntaxNode? current = node.Parent; |
| 24 | + |
| 25 | + while (current is not null) |
| 26 | + { |
| 27 | + if (current is TypeDeclarationSyntax typeDecl) |
| 28 | + { |
| 29 | + return typeDecl; |
| 30 | + } |
| 31 | + |
| 32 | + current = current.Parent; |
| 33 | + } |
| 34 | + |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + private static bool IsObjectField(string fieldName, TypeDeclarationSyntax enclosingType) |
| 39 | + { |
| 40 | + foreach (MemberDeclarationSyntax member in enclosingType.Members) |
| 41 | + { |
| 42 | + if (member is not FieldDeclarationSyntax field) |
| 43 | + { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + foreach (VariableDeclaratorSyntax variable in field.Declaration.Variables) |
| 48 | + { |
| 49 | + if (variable.Identifier.Text == fieldName && IsObjectType(field.Declaration.Type)) |
| 50 | + { |
| 51 | + return true; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + public bool IsEnabled(LintConfiguration configuration) => |
| 60 | + configuration.GetValue("csharp_prefer_system_threading_lock") is not null; |
| 61 | + |
| 62 | + public IReadOnlyList<LintDiagnostic> Analyze(RuleContext context) |
| 63 | + { |
| 64 | + (string? pref, string? _) = context.Configuration |
| 65 | + .GetValueWithSeverity("csharp_prefer_system_threading_lock"); |
| 66 | + |
| 67 | + if (!string.Equals(pref, "true", StringComparison.OrdinalIgnoreCase)) |
| 68 | + { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + var diagnostics = new List<LintDiagnostic>(); |
| 73 | + |
| 74 | + foreach (SyntaxNode node in context.Root.DescendantNodes()) |
| 75 | + { |
| 76 | + VisitNode(node, context.Configuration, context.FilePath, diagnostics); |
| 77 | + } |
| 78 | + |
| 79 | + return diagnostics; |
| 80 | + } |
| 81 | + |
| 82 | + public void VisitNode( |
| 83 | + SyntaxNode node, |
| 84 | + LintConfiguration config, |
| 85 | + string filePath, |
| 86 | + List<LintDiagnostic> diagnostics) |
| 87 | + { |
| 88 | + if (node is not LockStatementSyntax lockStatement) |
| 89 | + { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + (string? pref, string? _) = config.GetValueWithSeverity("csharp_prefer_system_threading_lock"); |
| 94 | + |
| 95 | + if (!string.Equals(pref, "true", StringComparison.OrdinalIgnoreCase)) |
| 96 | + { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + ExpressionSyntax expression = lockStatement.Expression; |
| 101 | + string? message = GetDiagnosticMessage(expression); |
| 102 | + |
| 103 | + if (message is null) |
| 104 | + { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + FileLinePositionSpan span = lockStatement.LockKeyword.GetLocation().GetLineSpan(); |
| 109 | + |
| 110 | + diagnostics.Add( |
| 111 | + new LintDiagnostic |
| 112 | + { |
| 113 | + RuleId = RuleId, |
| 114 | + Message = message, |
| 115 | + Severity = LintSeverity.Info, |
| 116 | + FilePath = filePath, |
| 117 | + Line = span.StartLinePosition.Line + 1, |
| 118 | + Column = span.StartLinePosition.Character + 1, |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + private string? GetDiagnosticMessage(ExpressionSyntax expression) |
| 123 | + { |
| 124 | + // lock (this) |
| 125 | + if (expression is ThisExpressionSyntax) |
| 126 | + { |
| 127 | + return "Use a dedicated System.Threading.Lock field instead of locking on 'this'"; |
| 128 | + } |
| 129 | + |
| 130 | + // lock (typeof(...)) |
| 131 | + if (expression is TypeOfExpressionSyntax) |
| 132 | + { |
| 133 | + return "Use a dedicated System.Threading.Lock field instead of locking on a Type object"; |
| 134 | + } |
| 135 | + |
| 136 | + // Extract field name from identifier or this.identifier |
| 137 | + string? fieldName = expression switch |
| 138 | + { |
| 139 | + IdentifierNameSyntax id => id.Identifier.Text, |
| 140 | + MemberAccessExpressionSyntax { Expression: ThisExpressionSyntax, Name: IdentifierNameSyntax name } => name.Identifier.Text, |
| 141 | + _ => null, |
| 142 | + }; |
| 143 | + |
| 144 | + if (fieldName is null) |
| 145 | + { |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + TypeDeclarationSyntax? enclosingType = FindEnclosingType(expression); |
| 150 | + |
| 151 | + if (enclosingType is null) |
| 152 | + { |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + if (IsObjectField(fieldName, enclosingType)) |
| 157 | + { |
| 158 | + return $"Use System.Threading.Lock instead of object for lock field '{fieldName}'"; |
| 159 | + } |
| 160 | + |
| 161 | + return null; |
| 162 | + } |
| 163 | +} |
0 commit comments