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

Analyzer to guard against comparing struct to null #3986

Closed
AmadeusW opened this issue Aug 10, 2020 · 5 comments
Closed

Analyzer to guard against comparing struct to null #3986

AmadeusW opened this issue Aug 10, 2020 · 5 comments

Comments

@AmadeusW
Copy link
Contributor

AmadeusW commented Aug 10, 2020

Describe the problem you are trying to solve

We checked if a variable of struct type is null. The check was negative, and we took incorrect code path.
We should have checked if the variable is default. This would have resulted in positive check, and we would have taken correct code path.

I would like to guard against such with an analyzer.
Does an analyzer like this already exist?

Describe suggestions on how to achieve the rule

  1. Look for comparison between value type and null (== null or != null)
  2. The fix is to replace null with default

I guess implementation could be similar to one of CA2242

Additional context

Sample scenario

struct MyStructWithObject
{
    object MyObject {get;}
    public myStruct(object o)
    {
        MyObject = o ?? throw new ArgumentNullException(nameof(o));
    }
}

// Code which triggers NullReferenceException
GetText(default);

// Analyzer should raise a warning in this method
string GetText(MyStructWithObject s)
{
    if (s == null) // Evaluates to false, branch not taken
    {
        return string.Empty;
    }
    return s.MyObject.ToString(); // Throws NullReferenceException
}

// Fixed
string GetText(MyStructWithObject s)
{
    if (s == default) // Fixed
    {
        return string.Empty;
    }
    return s.MyObject.ToString();
}
@mavasani
Copy link
Contributor

Tagging @jmarolf @gafter @jaredpar - I believe this is one of the diagnostic planned for compiler warning wave. If not, this should certainly be a good candidate for warning level analyzer diagnostic that ships with the .NET SDK.

@jmarolf
Copy link
Contributor

jmarolf commented Aug 10, 2020

I believe this is exactly encompasses dotnet/roslyn#45744 and is already checked into roslyn master

@mavasani
Copy link
Contributor

Great, closing this as external!

@AmadeusW
Copy link
Contributor Author

Thanks @jmarolf! I read through the PR you linked and its linked PRs, and I'm curious how to enable this.

  • Will compiler shipping with VS 16.8 allow me to use <AnalysisLevel>5</AnalysisLevel>?
  • Is there a way to opt into seeing CS8073 on a build machine which hasn't been updated to VS 16.8? Is <Features>strict</Features>the way to go?
  • Additionally, do I need to enable nullability to see CS8073?

@jmarolf
Copy link
Contributor

jmarolf commented Aug 11, 2020

<WarningLevel>5</WarningLevel> is probably the way to go. I have no idea how CoreXT targets work so I am unable to speculate what other msbuild options will function correctly. <AnalysisLevel>5</AnalysisLevel> is an alias for this that also enable .NET platform analyzers. If all you care about is getting the new warnings from the compiler setting WarningLevel to 5 should do what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants