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

SA1119 is broken with pattern matching #2570

Closed
nicholassuter opened this issue Nov 29, 2017 · 4 comments
Closed

SA1119 is broken with pattern matching #2570

nicholassuter opened this issue Nov 29, 2017 · 4 comments
Assignees

Comments

@nicholassuter
Copy link

nicholassuter commented Nov 29, 2017

This statement generates a violation of SA1119

var x = new object();
if (!(x is int i))
    throw new Exception("Woopsie");
Console.WriteLine("is an int");

The proposed fix is

var x = new object();
if (!x is int i)
    throw new Exception("Woopsie");
Console.WriteLine("is an int");

This generates a CS0023 (Operator '!' cannot be applied to operand of type 'object') compiler error.

The workaround is disabling this rule (or not using the StyleCop analyzer...)

@TimSirmovics
Copy link

I am also experiencing this issue.

This code triggers SA1119:

if (!(WebRequest.Create(fileUri) is FtpWebRequest ftpWebRequest))
    throw new InvalidLogicException("Cannot cast WebRequest as FtpWebRequest");

But removing the paranthesis changes the meaning of the code and causes a compiler error.

@vweijsters
Copy link
Contributor

@nicholassuter and @TimSirmovics: Which version of StyleCop.Analyzers produced this result? I've tried the provided examples with version 1.1.0-beta004 and that is working as intended, not producing any warnings.

@TimSirmovics
Copy link

TimSirmovics commented Dec 10, 2017

Latest stable, v1.0.2

It appears this has been addressed in #2372

@sharwell
Copy link
Member

Duplicate of #2372

@sharwell sharwell marked this as a duplicate of #2372 Jan 17, 2018
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

4 participants