-
Notifications
You must be signed in to change notification settings - Fork 232
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
94dde8e
commit 86e1ba7
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
analyzers/tests/SonarAnalyzer.UnitTest/Rules/SymbolicExecution/CollectionConstraintTests.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,62 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
using ChecksCS = SonarAnalyzer.SymbolicExecution.Roslyn.RuleChecks.CSharp; | ||
using CS = SonarAnalyzer.Rules.CSharp; | ||
|
||
namespace SonarAnalyzer.UnitTest.Rules; | ||
|
||
[TestClass] | ||
public class CollectionConstraintTests | ||
{ | ||
private readonly VerifierBuilder builder = new VerifierBuilder() | ||
.AddAnalyzer(() => new CS.SymbolicExecutionRunner(AnalyzerConfiguration.AlwaysEnabled)) | ||
.WithOnlyDiagnostics(ChecksCS.ConditionEvaluatesToConstant.S2589, ChecksCS.EmptyCollectionsShouldNotBeEnumerated.S4158); | ||
|
||
[DataTestMethod] | ||
[DataRow("List", "Add", "Remove(1)")] | ||
[DataRow("List", "Add", "RemoveAll(x => true)")] | ||
[DataRow("List", "Add", "RemoveAt(1)")] | ||
[DataRow("List", "Add", "RemoveRange(1, 1)")] | ||
[DataRow("HashSet", "Add", "ExceptWith(null)")] | ||
[DataRow("HashSet", "Add", "IntersectWith(null)")] | ||
[DataRow("HashSet", "Add", "RemoveWhere(x => true)")] | ||
[DataRow("Queue", "Enqueue", "Dequeue()")] | ||
[DataRow("Stack", "Push", "Pop()")] | ||
public void CollectionConstraint_RemoveMethods_RemoveNotNull(string type, string add, string remove) => | ||
builder.AddSnippet($$""" | ||
using System; | ||
using System.Collections.Generic; | ||
class Sample | ||
{ | ||
void Foo({{type}}<int> items) | ||
{ | ||
items.{{add}}(1); // Adds NotNull | ||
if (items.Count == 0) // Noncompliant 2589 always false | ||
return; | ||
items.{{remove}}; // Removes NotNull | ||
if (items.Count == 0) // Noncompliant FP | ||
return; | ||
Console.WriteLine(); | ||
} | ||
} | ||
""") | ||
.Verify(); | ||
} |