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

S2589: Add repros #8014

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Tests.Diagnostics
Expand Down Expand Up @@ -241,3 +242,52 @@ static void Demo(object demo)
}
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/8011
namespace Repro_8011
{
[Flags]
public enum Flags
{
None,
Foo,
Bar
}

public class VarWhen
{
public int Test(Flags flags)
{
return flags switch
{
var value when value.HasFlag(Flags.Foo) => 1, // Noncompliant FP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please assert also the message for these issues.
It will speed up our understanding upon tackling it.

//^^^^^^^^^

var value when value.HasFlag(Flags.Bar) => 2, // Noncompliant FP
//^^^^^^^^^
_ => 0
};
}
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/8008
namespace Repro_8008
{
class Person
{
public int? Age { get; set; }
}
// ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: just line break here is enough.

Suggested change
// ...

class Double_Wildcard
{
public int Compare(Person x, Person y) => (x.Age, y.Age) switch
{
(null, null) => 0,
(null, _) => 1,
(_, null) => -1,
(_, _) => Comparer<int>.Default.Compare(x.Age.Value, y.Age.Value) // Noncompliant FP
//^^^^^^
};
}
}