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

CSIsNull002 - Bad code fix offered in Linq to sql expression #31

Open
elachlan opened this issue Nov 22, 2022 · 5 comments
Open

CSIsNull002 - Bad code fix offered in Linq to sql expression #31

elachlan opened this issue Nov 22, 2022 · 5 comments

Comments

@elachlan
Copy link

I have a linq query from a DbContext with the following in the where clause:

(toDate != null || a.Date <= toDate)

toDate is a nullable date. So we check it for null prior to doing a filter using it.

CSIsNull002 matches to this and suggests is not null, but when the refactor is made, I get the error:

CS8122	An expression tree may not contain an 'is' pattern-matching operator.
@elachlan
Copy link
Author

This is a bad example because the logic is wrong, but it does result in the error.

@AArnott
Copy link
Owner

AArnott commented Nov 22, 2022

This seems very similar to #3, and I'm surprised this is still a bug since that was supposedly fixed.

@elachlan
Copy link
Author

@AArnott I ran into this again, are you able to have a look?

@AArnott AArnott self-assigned this Sep 27, 2023
@AArnott
Copy link
Owner

AArnott commented Sep 27, 2023

@elachlan what version are you using?

We have what looks like a test for this case already:

[Fact]
public async Task NotEqualsNullInExpressionTree_OffersOneCodeFix()
{
string source = @"
using System;
using System.Linq.Expressions;
class Test
{
void Method()
{
_ = (Expression<Func<string, bool>>)(s => s [|!= null|]);
}
}";
string fixedSource = @"
using System;
using System.Linq.Expressions;
class Test
{
void Method()
{
_ = (Expression<Func<string, bool>>)(s => s is object);
}
}";
await VerifyCS.VerifyCodeFixAsync(source, fixedSource, CSIsNull002Fixer.IsObjectEquivalenceKey);
await VerifyCS.VerifyCodeFixAsync(source, source, CSIsNull002Fixer.IsNotNullEquivalenceKey); // assert that this fix is not offered.
}

I even added a new test with compound logic to match yours, but it still did not offer a is not null code fix:

    [Fact]
    public async Task NotEqualsNullInExpressionTree_ProducesNoDiagnostic()
    {
        string source = @"
using System;
using System.Linq.Expressions;
class Test
{
    void Method(int o)
    {
        bool? b = true;
        Expression<Func<bool>> e = () => (b != null || 3 < 5);
    }
}";

        await VerifyCS.VerifyAnalyzerAsync(source);
    }

Now, in that case, no diagnostic was created either, which isn't good and I'm looking into that. But it's not the issue you're reporting.

@AArnott
Copy link
Owner

AArnott commented Sep 27, 2023

#70 is the fix to what I found, but again, that isn't the issue you're reporting.

@AArnott AArnott changed the title CSIsNull002 - False positive in Linq to sql expression CSIsNull002 - Bad code fix offered in Linq to sql expression Sep 27, 2023
@AArnott AArnott removed their assignment Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants