Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S1125 FP: Type check with System.Object #7792

Closed
kimpenhaus opened this issue Aug 15, 2023 · 1 comment · Fixed by #8575
Closed

Fix S1125 FP: Type check with System.Object #7792

kimpenhaus opened this issue Aug 15, 2023 · 1 comment · Fixed by #8575
Assignees
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@kimpenhaus
Copy link

Description

I'm not sure if this is by intent but I think there is an issue with recognizing S1125 Boolean literals should not be redundant when source type is not bool.

Repro steps

var exception = new Exception();
exception.Data.Add("handled", true);

if (exception.Data.Contains("handled") && exception.Data["handled"] is true)
{
    // do something
}

Data is of type IDictionary - which has kay/value as object. Therefore I think is true pattern matching is valid at this point. If I am wrong please correct me :)

image

Removing the pattern matching results in code that can not be compiled

image

Expected behavior

My expectation would be, that that pattern matching is valid.

Actual behavior

Actual behavior is a warning - which leads to build error when having warnings as errors enabled.

Known workarounds

Using as operator for converting doesn't give a warning:

var exception = new Exception();
exception.Data.Add("handled", true);

if (exception.Data.Contains("handled") && (exception.Data["handled"] as bool?) == true)
{
    // do something
}
image

Related information

  • C#/VB.NET Plugins version: 9.7.0.75501
  • Visual Studio version <-- None ... using Rider 2023.2
  • MSBuild / dotnet version: 7.0.400
  • SonarScanner for .NET version (if used)
  • Operating System: OSX Ventura 13.5
@kimpenhaus kimpenhaus changed the title Fix S1125 FP/FN: is true - only if source type is bool? Fix S1125 FP/FN: is true - only if source type is bool Aug 15, 2023
@zsolt-kolbay-sonarsource
Copy link
Contributor

Thank you for reporting the issue @kimpenhaus. Confirmed as False Positive.

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource removed their assignment Aug 17, 2023
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels Aug 17, 2023
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource changed the title Fix S1125 FP/FN: is true - only if source type is bool Fix S1125 FP: Type check with System.Object Aug 17, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource added the Sprint: Hardening Fix FPs/FNs/improvements label Jan 22, 2024
@martin-strecker-sonarsource martin-strecker-sonarsource added this to the 9.18 milestone Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants