Skip to content

Commit

Permalink
Fix ITs after change of messages
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioaversa committed Feb 27, 2023
1 parent 0162b48 commit 829c892
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 6 deletions.
Expand Up @@ -2,7 +2,7 @@
"issues": [
{
"id": "S2445",
"message": "'typeMap' is a local variable, and should not be used for locking.",
"message": "Do not lock on local variable 'typeMap', use a readonly field instead.",
"location": {
"uri": "sources\Automapper\src\AutoMapper\Configuration\MapperConfiguration.cs",
"region": {
Expand Down
Expand Up @@ -2,7 +2,7 @@
"issues": [
{
"id": "S2445",
"message": "'typeMap' is a local variable, and should not be used for locking.",
"message": "Do not lock on local variable 'typeMap', use a readonly field instead.",
"location": {
"uri": "sources\Automapper\src\AutoMapper\Configuration\MapperConfiguration.cs",
"region": {
Expand Down
2 changes: 1 addition & 1 deletion analyzers/its/expected/Nancy/Nancy--net452-S2445.json
Expand Up @@ -2,7 +2,7 @@
"issues": [
{
"id": "S2445",
"message": "'entitiesLock' is not 'private readonly', and should not be used for locking.",
"message": "Do not lock on writable field 'entitiesLock', use a readonly field instead.",
"location": {
"uri": "sources\Nancy\src\Nancy\Helpers\HttpEncoder.cs",
"region": {
Expand Down
Expand Up @@ -2,7 +2,7 @@
"issues": [
{
"id": "S2445",
"message": "'entitiesLock' is not 'private readonly', and should not be used for locking.",
"message": "Do not lock on writable field 'entitiesLock', use a readonly field instead.",
"location": {
"uri": "sources\Nancy\src\Nancy\Helpers\HttpEncoder.cs",
"region": {
Expand Down
Expand Up @@ -50,7 +50,7 @@ private static void CheckLockStatement(SonarSyntaxNodeReportingContext context)
{
ReportIssue($"local variable '{lockedSymbol.Name}'");
}
else if (FieldNotReadonly(expression, lazySymbol) is { } lockedField)
else if (FieldWritable(expression, lazySymbol) is { } lockedField)
{
ReportIssue($"writable field '{lockedField.Name}'");
}
Expand All @@ -72,7 +72,7 @@ private static void CheckLockStatement(SonarSyntaxNodeReportingContext context)
expression.IsAnyKind(SyntaxKind.StringLiteralExpression, SyntaxKind.InterpolatedStringExpression)
|| lazySymbol.Value.GetSymbolType().Is(KnownType.System_String);

private static IFieldSymbol FieldNotReadonly(ExpressionSyntax expression, Lazy<ISymbol> lazySymbol) =>
private static IFieldSymbol FieldWritable(ExpressionSyntax expression, Lazy<ISymbol> lazySymbol) =>
expression.IsAnyKind(SyntaxKind.IdentifierName, SyntaxKind.SimpleMemberAccessExpression) && lazySymbol.Value is IFieldSymbol lockedField && !lockedField.IsReadOnly
? lockedField
: null;
Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

class Test
Expand Down Expand Up @@ -64,6 +65,14 @@ void OnALocalVariable()
lock (localVarReadWriteField) { } // Noncompliant
}

void OnALocalOutVar(Dictionary<int, object> lockObjs)
{
if (lockObjs.TryGetValue(42, out var lockObj))
{
lock (lockObj) { } // Noncompliant, FP: the lock object is a local variable retrieved from a collection of locks
}
}

void OnANewInstance()
{
lock (new object()) { } // Noncompliant {{Do not lock on a new instance because is a no-op, use a readonly field instead.}}
Expand Down

0 comments on commit 829c892

Please sign in to comment.