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 false positives on System.Range syntax #3370

Closed
ZacharyPatten opened this issue Jul 17, 2021 · 3 comments · Fixed by #3372
Closed

SA1119 false positives on System.Range syntax #3370

ZacharyPatten opened this issue Jul 17, 2021 · 3 comments · Fixed by #3372

Comments

@ZacharyPatten
Copy link

ZacharyPatten commented Jul 17, 2021

SA1119 is giving false positives for System.Range syntax. If you make an extension method on System.Range (perhaps something like ToArray()), you must wrap the range syntax in parentheses because method calls are higher priority than range syntax. Example:

using System;

int[] array = (1..10).ToArray(); // <- false positive SA1119

static class C
{
    public static int[] ToArray(this Range range) => default;
}

The parentheses are required in the above example, but SA1119 thinks they are not necessary, and when you perform the auto-correct feature of SA1119 it removes the parentheses breaking your code with a compiler error.

I ran into this issue with my extension methods here:
https://github.com/ZacharyPatten/Towel/blob/3e2509e887ddfacb095c9c38efdbd1deea2ec4cd/Sources/Towel/Extensions.cs#L668

@FreeApophis
Copy link

FreeApophis commented Sep 9, 2021

I ran into a similar issue, but in this case it is the from operator with the precedence issue.
Without the paranthesis you get this error:

CS8848 Operator 'from' cannot be used here due to precedence.

from x in (12..)
from y in  1..3
select x + y;

@sharwell sharwell added the bug label Sep 10, 2021
@pantosha
Copy link
Contributor

@FreeApophis could you provide more context for your code snippet?
I'm trying to reproduce, but get error CS0029: Cannot implicitly convert type 'System.Range' to 'System.Collections.Generic.IEnumerable<int>'

using System;
using System.Collections.Generic;

public class C {
    public void M() {
        var z = from x in (12..)
            from y in 1..3
            select x + y;
    }
}

public static class RangeExtensions
{    
    public static IEnumerable<int> SelectMany(this Range range, Func<Range, IEnumerable<int>> func)
    {
        return new int[] {0};
    }
}

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEA3GUCWAZgJ5oAmIA1AD4ACATAIwCwAULQAwAEtjKA3Gza0AzD3pcAwlwDebLgp5jaKLgFkAFAEpZ8xfuwBDKFwBeXALxdCUCAFsuCLvgB2XDY3oA6L1r36A61sHYmc3Rh8Rf0DFAGcYABsYMAxHLkouYkFWfQBfNnzWYWVGJHEuACVDFwBzGABRBAwYF1j8CFa2GX1o0R5S/pEAHlcMAD4uAGVE5Iw1auINDAALfFjK6rquKE2YNB4AViGq2r3BkZdxicIAVxcwPxzFOSfA2gB2LhcYAHcwjAA2gBdWQcXLZPIFIA==

@FreeApophis
Copy link

@FreeApophis could you provide more context for your code snippet?

Yes, the typesystem needs a bit more than just that to make it work.

Here is the changeset which makes it possible to use ranges in foreach and from clauses:
polyadic/funcky@35777b1#diff-1251c8804032a77389c8d833580d16f2dadfeb587770ab6de2865ae2e836763c

The Funcky version is not yet released with this feature in it, but this pre-release should have the feature:
https://github.com/polyadic/funcky/packages/801877

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

Successfully merging a pull request may close this issue.

4 participants