Skip to content

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

@kimpenhaus

Description

@kimpenhaus

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

Metadata

Metadata

Labels

False PositiveRule IS triggered when it shouldn't be.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions