-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
cc54771
commit e4282e7
Showing
10 changed files
with
4,183 additions
and
8 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
analyzers/src/SonarAnalyzer.CSharp/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.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,29 @@ | ||
/* | ||
* 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.SymbolicExecution.Roslyn.RuleChecks.CSharp; | ||
|
||
public class ConditionEvaluatesToConstant : ConditionEvaluatesToConstantBase | ||
{ | ||
public static readonly DiagnosticDescriptor S2589 = DescriptorFactory.Create(DiagnosticIdCodeSmell, MessageFormat); | ||
public static readonly DiagnosticDescriptor S2583 = DescriptorFactory.Create(DiagnosticIdBug, MessageFormat); | ||
protected override DiagnosticDescriptor Rule => null; | ||
public override bool ShouldExecute() => false; | ||
} |
31 changes: 31 additions & 0 deletions
31
...arAnalyzer.Common/SymbolicExecution/Roslyn/RuleChecks/ConditionEvaluatesToConstantBase.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,31 @@ | ||
/* | ||
* 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.SymbolicExecution.Roslyn.RuleChecks; | ||
|
||
public abstract class ConditionEvaluatesToConstantBase : SymbolicRuleCheck | ||
{ | ||
protected const string DiagnosticIdCodeSmell = "S2589"; // Code smell | ||
protected const string DiagnosticIdBug = "S2583"; // Bug | ||
|
||
protected const string MessageFormat = "{0}"; | ||
protected const string MessageFormatBool = "Change this condition so that it does not always evaluate to '{0}'."; | ||
protected const string MessageNull = "Change this expression which always evaluates to 'null'."; | ||
} |
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
28 changes: 28 additions & 0 deletions
28
...yzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp10.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,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SonarAnalyzer.UnitTest.TestCases; | ||
|
||
struct MyStruct | ||
{ | ||
public bool x; | ||
public bool y = false; | ||
|
||
public MyStruct() | ||
{ | ||
x = false; | ||
} | ||
} | ||
|
||
class AClass | ||
{ | ||
public static void DoSomething() | ||
{ | ||
MyStruct myStruct = new(); | ||
if (myStruct.x) { } // FN | ||
else if (myStruct.y) { } // FN | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...yzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.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,43 @@ | ||
namespace SonarAnalyzer.UnitTest.TestCases | ||
{ | ||
class AClass | ||
{ | ||
void DoSomething() { } | ||
|
||
void ListPattern() | ||
{ | ||
int[] a = new int[] { 1, 2, 3 }; | ||
if (a is [1, 2, 3]) // FN | ||
{ | ||
DoSomething(); | ||
} | ||
} | ||
|
||
void ListPattern2() | ||
{ | ||
int[] a = new int[] { 1, 2, 3 }; | ||
if (a is [1, _, 3]) // FN | ||
{ | ||
DoSomething(); | ||
} | ||
} | ||
|
||
void ListPattern3() | ||
{ | ||
int[] a = new int[] { 1, 2, 3 }; | ||
if (a is [1, .., 3]) // FN | ||
{ | ||
DoSomething(); | ||
} | ||
} | ||
|
||
void ListPattern4() | ||
{ | ||
int[] a = new int[] { 1, 2, 3 }; | ||
if (a is [> 0, < 3, < 42]) // FN | ||
{ | ||
DoSomething(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.