Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c33344
commit 0479b43
Showing
9 changed files
with
312 additions
and
66 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
61 changes: 61 additions & 0 deletions
61
analyzers/src/SonarAnalyzer.CSharp/Rules/LockedFieldAssignment.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,61 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2023 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
namespace SonarAnalyzer.Rules.CSharp; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class LockedFieldAssignment : SonarDiagnosticAnalyzer | ||
{ | ||
private const string DiagnosticId = "S2445"; | ||
|
||
private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, "{0}"); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
protected override void Initialize(SonarAnalysisContext context) => | ||
context.RegisterNodeAction(c => | ||
{ | ||
var expression = ((LockStatementSyntax)c.Node).Expression; | ||
if (expression is ObjectCreationExpressionSyntax | ||
or AnonymousObjectCreationExpressionSyntax | ||
or ArrayCreationExpressionSyntax | ||
or ImplicitArrayCreationExpressionSyntax | ||
or QueryExpressionSyntax) | ||
{ | ||
c.ReportIssue(Diagnostic.Create(Rule, expression.GetLocation(), "Locking on a new instance is a no-op.")); | ||
} | ||
else if (expression.IsAnyKind(SyntaxKind.StringLiteralExpression, SyntaxKind.InterpolatedStringExpression)) | ||
{ | ||
c.ReportIssue(Diagnostic.Create(Rule, expression.GetLocation(), "Strings can be interned, and should not be used for locking.")); | ||
} | ||
else if (expression is IdentifierNameSyntax | ||
&& c.SemanticModel.GetSymbolInfo(expression).Symbol is ILocalSymbol lockedSymbol) | ||
{ | ||
c.ReportIssue(Diagnostic.Create(Rule, expression.GetLocation(), $"'{lockedSymbol.Name}' is a local variable, and should not be used for locking.")); | ||
} | ||
else if (expression is (IdentifierNameSyntax or MemberAccessExpressionSyntax) | ||
&& c.SemanticModel.GetSymbolInfo(expression).Symbol is IFieldSymbol lockedField | ||
&& !lockedField.IsReadOnly) | ||
{ | ||
c.ReportIssue(Diagnostic.Create(Rule, expression.GetLocation(), $"'{lockedField.Name}' is not 'private readonly', and should not be used for locking.")); | ||
} | ||
}, | ||
SyntaxKind.LockStatement); | ||
} |
43 changes: 0 additions & 43 deletions
43
analyzers/src/SonarAnalyzer.CSharp/Rules/SynchronizedFieldAssignment.cs
This file was deleted.
Oops, something went wrong.
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
21 changes: 21 additions & 0 deletions
21
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/LockedFieldAssignment.CSharp11.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,21 @@ | ||
using System; | ||
|
||
class Test | ||
{ | ||
static readonly object staticReadonlyField = null; | ||
static object staticReadWriteField = null; | ||
|
||
readonly object readonlyField = null; | ||
object readWriteField = null; | ||
|
||
void OnANewInstance() | ||
{ | ||
lock ("""a raw string literal""") { } // Noncompliant | ||
lock ($"""an interpolated {"raw string literal"}""") { } // Noncompliant | ||
} | ||
|
||
void TargetTypedObjectCreation() | ||
{ | ||
lock (new() as Tuple<int>) { } // Error [CS8754] | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/LockedFieldAssignment.CSharp8.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,43 @@ | ||
using System; | ||
|
||
class Test | ||
{ | ||
static readonly object staticReadonlyField = null; | ||
static object staticReadWriteField = null; | ||
|
||
readonly object readonlyField = null; | ||
object readWriteField = null; | ||
|
||
Test() | ||
{ | ||
ref object refToReadonlyField = ref readonlyField; | ||
lock (refToReadonlyField) { } // Noncompliant, while the reference is to a readonly field, the reference itself is a local variable and as of C# 7.3 can be ref reassigned | ||
|
||
ref object refToReadWriteField = ref readWriteField; | ||
lock (refToReadWriteField) { } // Noncompliant | ||
} | ||
|
||
void ReadonlyReferences() | ||
{ | ||
lock (RefReturnReadonlyField(this)) { } | ||
lock (RefReturnStaticReadonlyField()) { } | ||
lock (StaticRefReturnReadonlyField(this)) { } | ||
lock (StaticRefReturnStaticReadonlyField()) { } | ||
|
||
ref readonly object RefReturnReadonlyField(Test instance) => ref instance.readonlyField; | ||
ref readonly object RefReturnStaticReadonlyField() => ref Test.staticReadonlyField; | ||
static ref readonly object StaticRefReturnReadonlyField(Test instance) => ref instance.readonlyField; | ||
static ref readonly object StaticRefReturnStaticReadonlyField() => ref Test.staticReadonlyField; | ||
} | ||
|
||
void OnANewInstanceOnStack() | ||
{ | ||
lock (stackalloc int[] { }) { } // Error [CS0185] | ||
lock (stackalloc [] { 1 }) { } // Error [CS0185] | ||
} | ||
|
||
void CoalescingAssignment(object oPar) | ||
{ | ||
lock (oPar ??= readonlyField) { } // FN, null conditional assignment not supported | ||
} | ||
} |
Oops, something went wrong.