Skip to content

Commit

Permalink
Rule S6612: Fix unit tests (#7243)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristian-ambrosini-sonarsource authored May 22, 2023
1 parent 5d5c164 commit 91059f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ protected override bool IsLambdaAndContainsIdentifier(ArgumentSyntax argument, s
argument.Expression switch
{
SimpleLambdaExpressionSyntax simpleLambda =>
simpleLambda.Body.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Any(p => p.GetName().Equals(keyName)),
!simpleLambda.Parameter.GetName().Equals(keyName)
&& simpleLambda.Body.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Any(p => p.GetName().Equals(keyName)),
ParenthesizedLambdaExpressionSyntax parentesizedLambda =>
parentesizedLambda.Body.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Any(p => p.GetName().Equals(keyName)),
!parentesizedLambda.ParameterList.Parameters.Any(x => x.GetName().Equals(keyName))
&& parentesizedLambda.Body.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Any(p => p.GetName().Equals(keyName)),
AnonymousMethodExpressionSyntax anonymousMethod =>
anonymousMethod.Block.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Any(p => p.GetName().Equals(keyName)),
!anonymousMethod.ParameterList.Parameters.Any(x => x.GetName().Equals(keyName))
&& anonymousMethod.Block.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Any(p => p.GetName().Equals(keyName)),
_ => false
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ namespace SonarAnalyzer.UnitTest.Rules;
public class UseLambdaParameterInConcurrentDictionaryTest
{
[TestMethod]
public void UseLambdaParameterInConcurrentDictionary_CS() =>
new VerifierBuilder<CS.UseLambdaParameterInConcurrentDictionary>().AddPaths("UseLambdaParameterInConcurrentDictionary.cs")
public void UseLambdaParameterInConcurrentDictionary_CSharp8() =>
new VerifierBuilder<CS.UseLambdaParameterInConcurrentDictionary>().AddPaths("UseLambdaParameterInConcurrentDictionary.CSharp8.cs")
.AddReferences(MetadataReferenceFacade.SystemCollections)
.WithOptions(ParseOptionsHelper.FromCSharp8)
.Verify();

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ void GetOrAdd(ConcurrentDictionary<int, int> dictionary, int key)
dictionary.GetOrAdd(key, _ => key); // Noncompliant

dictionary.GetOrAdd(42, _ => key + 42);
dictionary.GetOrAdd(key, key => key + 42); // Error [CS0136]
// Noncompliant@-1
dictionary.GetOrAdd(42, key => key + 42); // Error [CS0136]
dictionary.GetOrAdd(key, key => key + 42); // Compliant (the 'key' referenced is the lambda parameter)
dictionary.GetOrAdd(42, key => key + 42);
dictionary.GetOrAdd(key, delegate (int k) { return key + 42; }); // Noncompliant
dictionary.GetOrAdd(key, delegate (int key) { return key + 42; });

// GetOrAdd(TKey, TValue)
dictionary.GetOrAdd(42, 42);
Expand All @@ -28,8 +28,8 @@ void GetOrAdd(ConcurrentDictionary<int, int> dictionary, int key)
// ^^^^^^^^^^^^^^^^^^^^
dictionary.GetOrAdd(key, delegate (int _, int arg) { return key + 42; }, 42); // Noncompliant
dictionary.GetOrAdd(key, (_, arg) => arg + 42, 42);
dictionary.GetOrAdd(42, (key, arg) => arg + 42, 42); // Error [CS0136]
dictionary.GetOrAdd(key, (key, arg) => arg + 42, 42); // Error [CS0136]
dictionary.GetOrAdd(42, (key, arg) => arg + 42, 42);
dictionary.GetOrAdd(key, (key, arg) => arg + 42, 42);
}

void AddOrUpdate(ConcurrentDictionary<int, int> dictionary, int key)
Expand All @@ -42,14 +42,14 @@ void AddOrUpdate(ConcurrentDictionary<int, int> dictionary, int key)
// ^^^^^^^^^^^^^^^^^^^^^^^^^ @-1 {{Use the lambda parameter instead of capturing the argument 'key'}}
dictionary.AddOrUpdate(key, _ => 42, (_, oldValue) => oldValue + 42);
dictionary.AddOrUpdate(42, _ => 42, (_, oldValue) => oldValue + 42);
dictionary.AddOrUpdate(42, _ => 42, (key, oldValue) => key + 42); // Error [CS0136]
dictionary.AddOrUpdate(42, _ => 42, (key, oldValue) => key + 42);

// AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>)
dictionary.AddOrUpdate(key, 42, (_, oldValue) => key + 42); // Noncompliant
dictionary.AddOrUpdate(42, key, (_, oldValue) => key + 42);
dictionary.AddOrUpdate(42, 42, (_, oldValue) => key + 42);
dictionary.AddOrUpdate(key, key, (_, oldValue) => oldValue + 42);
dictionary.AddOrUpdate(42, 42, (key, oldValue) => key + 42); // Error [CS0136]
dictionary.AddOrUpdate(42, 42, (key, oldValue) => key + 42);
dictionary.AddOrUpdate(42, 42, (_, oldValue) => oldValue + 42);

// AddOrUpdate<TArg>(TKey, Func<TKey,TArg,TValue>, Func<TKey,TValue,TArg,TValue>, TArg)
Expand All @@ -62,9 +62,8 @@ void AddOrUpdate(ConcurrentDictionary<int, int> dictionary, int key)
dictionary.AddOrUpdate(42, (_, arg) => key, (_, oldValue, arg) => oldValue + arg, 42);
dictionary.AddOrUpdate(42, (_, arg) => arg, (_, oldValue, arg) => oldValue + arg, key);
dictionary.AddOrUpdate(42, (_, arg) => key, (_, oldValue, arg) => oldValue + arg, key);
dictionary.AddOrUpdate(key, (_, arg) => key, (_, key, arg) => arg, key); // Error [CS0136]
// Noncompliant@-1
dictionary.AddOrUpdate(42, (_, arg) => key, (_, key, arg) => key + arg, key); // Error [CS0136]
dictionary.AddOrUpdate(key, (_, arg) => key, (_, key, arg) => arg, key); // Noncompliant
dictionary.AddOrUpdate(42, (_, arg) => key, (_, key, arg) => key + arg, key);
}

void CompliantInvocations(ConcurrentDictionary<int, int> dictionary, HidesMethod<int, int> hidesMethod, List<int> list, int key)
Expand Down

0 comments on commit 91059f8

Please sign in to comment.