-
Notifications
You must be signed in to change notification settings - Fork 226
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
b171ec7
commit a140882
Showing
5 changed files
with
107 additions
and
58 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
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
19 changes: 19 additions & 0 deletions
19
...arAnalyzer.UnitTest/TestCases/ConditionalsWithSameCondition.CSharp9.TopLevelStatements.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,19 @@ | ||
using System; | ||
|
||
int a = 0, b = 1; | ||
|
||
if (a == b) { Foo(); } | ||
if (a == b) { Bar(); } | ||
|
||
if (args[0] == args[1]) | ||
{ | ||
Foo(); | ||
} | ||
|
||
if (args[0] == args[1]) // FN | ||
{ | ||
Bar(); | ||
} | ||
|
||
void Foo() { } | ||
void Bar() { } |
109 changes: 62 additions & 47 deletions
109
...er-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ConditionalsWithSameCondition.CSharp9.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 |
---|---|---|
@@ -1,53 +1,68 @@ | ||
string c = null; | ||
if (c is null) | ||
namespace Tests.TestCases | ||
{ | ||
doTheThing(c); | ||
} | ||
if (c is null) // FN | ||
{ | ||
doTheThing(c); | ||
} | ||
if (c is not null) // Compliant | ||
{ | ||
doTheThing(c); | ||
} | ||
class ConditionalsWithSameCondition | ||
{ | ||
public void Foo() | ||
{ | ||
string c = null; | ||
if (c is null) | ||
{ | ||
doTheThing(c); | ||
} | ||
if (c is null) // Noncompliant {{This condition was just checked on line 8.}} | ||
{ | ||
doTheThing(c); | ||
} | ||
if (c is not null) // Compliant | ||
{ | ||
doTheThing(c); | ||
} | ||
|
||
if (c is "banana") // Compliant, c might be updated in the previous if | ||
{ | ||
c += "a"; | ||
} | ||
if (c is "banana") // Compliant, c might be updated in the previous if | ||
{ | ||
c = ""; | ||
} | ||
if (c is "banana") // Compliant, c might be updated in the previous if | ||
{ | ||
c = ""; | ||
} | ||
if (c is "banana") // Compliant, c might be updated in the previous if | ||
{ | ||
c += "a"; | ||
} | ||
if (c is "banana") // Compliant, c might be updated in the previous if | ||
{ | ||
c = ""; | ||
} | ||
if (c is "banana") // Compliant, c might be updated in the previous if | ||
{ | ||
c = ""; | ||
} | ||
|
||
int i = 0; | ||
if (i is > 0 and < 100) | ||
{ | ||
doTheThing(i); | ||
} | ||
if (i is > 0 and < 100) // FN | ||
{ | ||
doTheThing(i); | ||
} | ||
int i = 0; | ||
if (i is > 0 and < 100) | ||
{ | ||
doTheThing(i); | ||
} | ||
if (i is > 0 and < 100) // Noncompliant {{This condition was just checked on line 35.}} | ||
{ | ||
doTheThing(i); | ||
} | ||
|
||
Fruit f = null; | ||
bool cond = false; | ||
if (f is Apple) | ||
{ | ||
f = cond ? new Apple() : new Orange(); | ||
} | ||
if (f is Apple) // Compliant as f may change | ||
{ | ||
f = cond ? new Apple() : new Orange(); | ||
} | ||
Fruit f = null; | ||
bool cond = false; | ||
if (f is Apple) | ||
{ | ||
f = cond ? new Apple() : new Orange(); | ||
} | ||
if (f is Apple) // Compliant as f may change | ||
{ | ||
f = cond ? new Apple() : new Orange(); | ||
} | ||
|
||
if (f is not null) { } | ||
if (f != null) { } // FN, same as above | ||
|
||
void doTheThing(object o) { } | ||
if (f is null) { } | ||
if (f == null) { } // FN, same as above | ||
} | ||
|
||
abstract class Fruit { } | ||
class Apple : Fruit { } | ||
class Orange : Fruit { } | ||
void doTheThing(object o) { } | ||
} | ||
|
||
abstract class Fruit { } | ||
class Apple : Fruit { } | ||
class Orange : Fruit { } | ||
} |