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

IDE0065 (Using directives must be placed outside of a namespace declaration) is noisy for alias declarations inside namespace #43271

Open
mavasani opened this issue Apr 10, 2020 · 9 comments
Assignees
Labels
Area-IDE Feature - IDE0065 Misplaced using directive Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Milestone

Comments

@mavasani
Copy link
Contributor

Version Used: Latest

Steps to Reproduce:

using System;

namespace N
{
    using GenericInt = GenericClass<int>;

    public static class A
    {
        public static void M(GenericInt g)
        {
            Console.WriteLine("Hello");
        }
    }

    public static class GenericClass<T>
    { 
    }
}

Expected Behavior:
Either IDE0065 does not fire or it has a code style option to allow alias declarations inside namespace. This is a very common pattern and is sometimes essential for correct type resolution.

Actual Behavior:
IDE0065 fires. This makes it very difficult to enforce this rule on CI.

@mavasani
Copy link
Contributor Author

@JoeRobich @sharwell

@sharwell
Copy link
Member

... is sometimes essential for correct type resolution

I don't mind requiring a suppression for this case. It means you've already introduced a naming ambiguity (though it's sometimes accidental or unavoidable in XML comments).

I'd love to have #27712 for this.

@mavasani
Copy link
Contributor Author

I don't mind requiring a suppression for this case.

This had 60+ violations in just the analyzers repo, and I suspect would have many more in Roslyn. This makes me feel like it would be noisy enough to warrant an additional option for aliases. We can wait for more customer feedback and decide though.

@sharwell
Copy link
Member

@mavasani I was only calling out the case where the alias must appear inside the namespace for correctness. I would be surprised if that was a majority case; generally the alias can be moved outside the namespace as long as the types are fully-qualified at the same time.

@CyrusNajmabadi
Copy link
Member

I would be surprised if that was a majority case; generally the alias can be moved outside the namespace as long as the types are fully-qualified at the same time.

tihs is true, but is often undesirably verbose.

I agree that using aliases should not necessarily be under the same purview as using directives. The directive is a global style on where people should place the namespaces they are pulling in.

The latter is used commonly for:

  1. disambiguation (in which case, grouping with the rest is appropriate)
  2. typedefs (in which case, there's no relation to the directives, and they should be placeable where conveinent).

@sharwell
Copy link
Member

sharwell commented Apr 10, 2020

I've had pull requests rejected from projects because I placed a using alias directive inside the namespace to avoid unnecessary verbosity. This analyzer and refactoring eliminated the tedious work of moving and qualifying it. It would be unfortunate to take that help away.

@mavasani
Copy link
Contributor Author

mavasani commented Apr 10, 2020

Can we add an option to exclude alias declarations from this rule? I think this is a classic case where there would be a proportionately divided group on whether or not they should be included in the rule, and it seems to justify an end user option.

@mavasani mavasani added the Need Design Review The end user experience design needs to be reviewed and approved. label Apr 10, 2020
@mavasani
Copy link
Contributor Author

Marking for design review.

@allisonchou
Copy link
Contributor

This issue was discussed during today's design meeting (4/13). It was decided that we will add an option to exclude using alias declarations, however the default configuration will be kept the same (i.e. applies to everything).

@allisonchou allisonchou removed the Need Design Review The end user experience design needs to be reviewed and approved. label Apr 13, 2020
@jinujoseph jinujoseph added this to the 16.7 milestone Apr 14, 2020
@mavasani mavasani added the IDE-CodeStyle Built-in analyzers, fixes, and refactorings label Jul 15, 2020
@jinujoseph jinujoseph modified the milestones: 16.7, Backlog Jul 26, 2020
@CyrusNajmabadi CyrusNajmabadi added the Feature - IDE0065 Misplaced using directive label Nov 1, 2022
@sharwell sharwell moved this to Complete in IDE: Design review Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Feature - IDE0065 Misplaced using directive Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Projects
Status: Complete
Development

No branches or pull requests

5 participants