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

Incorrect 'Use nameof' within nested types #229

Open
jnm2 opened this issue Mar 25, 2020 · 4 comments
Open

Incorrect 'Use nameof' within nested types #229

jnm2 opened this issue Mar 25, 2020 · 4 comments

Comments

@jnm2
Copy link
Collaborator

jnm2 commented Mar 25, 2020

This affects REFL016 and the fixes for others like REFL014.

There should be no warning here:

class Base
{
    protected void M() { }
}

class Derived : Base
{
    class Nested
    {
        void M()
        {
            // REFL016 Use nameof. ↓↓↓
            typeof(Base).GetMethod("M");
        }
    }
}

Result:

class Base
{
    protected void M() { }
}

class Derived : Base
{
    class Nested
    {
        void M()
        {
            // CS1540 Cannot access protected member 'Base.M()' via a qualifier of type 'Base';
            // the qualifier must be of type 'Derived.Nested' (or derived from it)
            //                                 ↓
            typeof(Base).GetMethod(nameof(Base.M));
        }
    }
}
@jnm2
Copy link
Collaborator Author

jnm2 commented Mar 25, 2020

Here is the fix for REFL014 making the same mistake.

Before:

using System.Reflection;

class Base
{
    protected bool M { get; }
}

class Derived : Base
{
    class Nested
    {
        void M()
        {
            // REFL014 Prefer typeof(Base).GetProperty(nameof(Base.M), BindingFlags.NonPublic |
            // BindingFlags.Instance | BindingFlags.DeclaredOnly).GetMethod.
            //           ↓↓↓↓↓↓↓↓↓
            typeof(Base).GetMethod("get_M", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        }
    }
}

After:

using System.Reflection;

class Base
{
    protected bool M { get; }
}

class Derived : Base
{
    class Nested
    {
        void M()
        {
            // CS1540 Cannot access protected member 'Base.M' via a qualifier of type 'Base';
            // the qualifier must be of type 'Derived.Nested' (or derived from it)
            //                                   ↓
            typeof(Base).GetProperty(nameof(Base.M), BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).GetMethod;
        }
    }
}

@JohanLarsson
Copy link
Collaborator

Hmm, it is somewhat annoying that we already have a check SemanticModel.IsAccessible(context.Node.SpanStart, member.Symbol) that I feel should avoid this.

@JohanLarsson
Copy link
Collaborator

The fix in the first post should be typeof(Base).GetMethod(nameof(C.M));

@jnm2
Copy link
Collaborator Author

jnm2 commented Mar 20, 2021

I still repro this in 0.1.22-dev, and the code snippet I originally reported repros in 0.1.22-dev too. The commit that closed this issue was included in the 0.1.22 tag, so I think this means that the issue is not fixed in the main branch.

@jnm2 jnm2 reopened this Mar 20, 2021
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