m-gallesio
(M Gallesio)
September 12, 2023, 8:40am
1
What language is this for?
C#
Which rule?
csharpsquid:S2583
Are you using
SonarQube Version 10.2 (build 77647)
This code (censored) which we use to fill in a table in a DOCX file:
const int itemsPerRow = 3;
var m = 0;
foreach (var item in items)
{
// do something
m++;
if (m % itemsPerRow == 0) // <- here
{
// do something
}
}
triggers this report:
Change this condition so that it does not always evaluate to ‘False’. Some code paths are unreachable.
The counter is incremented, so with enough items it will eventually satisfy the condition.
ttphan
(Tung Phan)
September 14, 2023, 1:39pm
3
Can confirm, this also triggers on tuple unpacking:
public void Test()
{
var x = 0;
(x, var y) = (Random.Shared.Next(), Random.Shared.Next());
if (x == 0) // Linter complains that this is always *True* instead of the expected False.
{
Console.WriteLine("hello world");
}
}
Hi @m-gallesio and @ttphan . Thank you for providing the code samples. Both are confirmed to be False Positives. I’ve created Github issues for them. If you wish to track the progress then subscribe to these issues:
opened 01:38PM - 18 Sep 23 UTC
Area: C#
Type: CFG/SE FPs
### Description
S2583 raises a warning for the following code:
```cs
publ… ic void Test()
{
var x = 0;
(x, var y) = (Random.Shared.Next(), Random.Shared.Next());
if (x == 0) // Noncompliant - FP
{
Console.WriteLine("hello world");
}
}
```
**Note:** if the x variable is also declared inside the tuple rather than in the previous line then the warning disappears:
```cs
(var x, var y) = (Random.Shared.Next(), Random.Shared.Next());
```
### Expected behavior
The analyzer should not raise an issue.
### Actual behavior
The analyzer raises an issue: `S2583: Change this condition so that it does not always evaluate to 'True'.`
### Related information
* C#/VB.NET Plugins version: 9.10.0.77988
* Visual Studio version: Visual Studio 2022 (version 17.7)
* MSBuild / dotnet version: .NET 7.0
* Operating System: Windows 10
opened 01:51PM - 18 Sep 23 UTC
Area: VB.NET
Area: C#
Type: CFG/SE FPs
### Description
S2583 raises a warning for the following code:
```cs
publ… ic void Test(int[] items)
{
var m = 0;
foreach (var item in items)
{
m++;
if (m % 3 == 0) // Noncompliant - FP
{
// do something
}
}
}
```
**Note:** the warning is not raised when 2 is used instead of 3 in the modulo operation:
```cs
...
if (m % 2 == 0) // Does not raise
...
```
### Expected behavior
The analyzer should not raise an issue.
### Actual behavior
The analyzer raises an issue: `S2583: Change this condition so that it does not always evaluate to 'False'.`
### Related information
* C#/VB.NET Plugins version: 9.10.0.77988
* Visual Studio version: Visual Studio 2022 (version 17.7)
* MSBuild / dotnet version: .NET 7.0
* Operating System: Windows 10
1 Like