Skip to content

Commit

Permalink
Fix S3247 FN: Support structs
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Jun 27, 2024
1 parent 0fc3570 commit 103bed9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ private static void IsExpression(SonarSyntaxNodeReportingContext analysisContext
var isExpression = (BinaryExpressionSyntax)analysisContext.Node;
if (isExpression.Right is TypeSyntax castType
&& isExpression.GetFirstNonParenthesizedParent() is IfStatementSyntax parentIfStatement
&& analysisContext.SemanticModel.GetSymbolInfo(castType).Symbol is INamedTypeSymbol castTypeSymbol
&& castTypeSymbol.TypeKind != TypeKind.Struct)
&& analysisContext.SemanticModel.GetSymbolInfo(castType).Symbol is INamedTypeSymbol castTypeSymbol)
{
ReportPatternAtMainVariable(analysisContext, isExpression.Left, isExpression.GetLocation(), parentIfStatement.Statement, castType, ReplaceWithAsAndNullCheckMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ class WithAliasAnyType
{
void ValidCases(Person person)
{
_ = (Person)person; // Compliant: not a duplicated cast
_ = (Person)person; // Compliant: not a duplicated cast
}

void InvalidCases(object obj)
{
if (obj is Person) // FN: Person is alias for a struct
if (obj is Person) // Noncompliant
{
_ = (Person)obj;
_ = (Person)obj; // Secondary
}

if (obj is (string, string)) // FN: (string, string) and Person are equivalent
if (obj is (string, string)) // FN: (string, string) and Person are equivalent
{
_ = (Person)obj;
}

if (obj is Person) // FN: Person and (string, string) are equivalent
if (obj is Person) // FN: Person and (string, string) are equivalent
{
_ = ((string, string))obj;
}

if (obj is Person) // FN: Person and (string ..., string) are equivalent
if (obj is Person) // FN: Person and (string ..., string) are equivalent
{
_ = ((string differentName1, string))obj;
}

if (obj is Person) // FN: Person and (string, string ...) are equivalent
if (obj is Person) // FN: Person and (string, string ...) are equivalent
{
_ = ((string, string differentName2))obj;
}
Expand All @@ -53,32 +53,32 @@ class Repro_223
{
void NumericTypes(object obj)
{
if (obj is int) // FN
if (obj is int) // Noncompliant
{
_ = (int)obj;
_ = (int)obj; // Secondary
}

if (obj is double) // FN
if (obj is double) // Noncompliant
{
_ = (double)obj;
_ = (double)obj; // Secondary
}

if (obj is ushort) // FN
if (obj is ushort) // Noncompliant
{
_ = (ushort)obj;
_ = (ushort)obj; // Secondary
}
}

void NullableValueTypes(object obj)
{
if (obj is int?) // FN
if (obj is int?) // Noncompliant
{
_ = (int?)obj;
_ = (int?)obj; // Secondary
}

if (obj is byte?) // FN
if (obj is byte?) // Noncompliant
{
_ = (byte?)obj;
_ = (byte?)obj; // Secondary
}
}

Expand Down Expand Up @@ -112,30 +112,30 @@ void UsingLanguageKeywordAndFrameworkName(object obj)

void Enums(object obj)
{
if (obj is AnEnum) // Noncompliant
if (obj is AnEnum) // Noncompliant
{
_ = (AnEnum)obj; // Secondary
_ = (AnEnum)obj; // Secondary
}

if (obj is AnEnum?) // FN
if (obj is AnEnum?) // Noncompliant
{
_ = (AnEnum?)obj;
_ = (AnEnum?)obj; // Secondary
}
}

void UserDefinedStructs(object obj)
{
if (obj is AStruct) // FN
if (obj is AStruct) // Noncompliant
{
_ = (AStruct)obj;
_ = (AStruct)obj; // Secondary
}

if (obj is ARecordStruct) // FN
if (obj is ARecordStruct) // Noncompliant
{
_ = (ARecordStruct)obj;
_ = (ARecordStruct)obj; // Secondary
}

if (obj is AReadonlyRefStruct) // FN
if (obj is AReadonlyRefStruct) // FN
{
_ = (AStruct)obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public void Bar(object x, object y)

public void FooBar(object x)
{
if (x is nuint)
if (x is nuint) // Noncompliant
{
var res = (nuint)x; // Compliant because we are casting to a ValueType
var res = (nuint)x; // Secondary
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;

class Fruit { }
struct SomeStruct { }

class Program
{
Expand Down Expand Up @@ -37,11 +38,16 @@ public void Bar(object x)

}

public void FooBar(object x)
public void WithStructs(object x)
{
if (x is int)
if (x is int) // Noncompliant
{
var res = (int)x; // Compliant because we are casting to a ValueType
var res = (int)x; // Secondary
}

if (x is SomeStruct) // Noncompliant
{
var res = (SomeStruct)x; // Secondary
}
}

Expand Down

0 comments on commit 103bed9

Please sign in to comment.