roslyn: `using` directives inside namespace blocks should be recognized as redundant with ImplicitUsings: enable

Version Used: VS 17.1 (31910.343.main)

Steps to Reproduce:

  1. Create a C# 10 console application with <ImplicitUsings>enable</ImplicitUsings> in the project file.
  2. Populate the Program.cs file with this:
    using System;
    
    namespace A
    {
        using System;
    
        public class C
        {
            static void Main()
            {
                String s = "Hi";
            }
        }
    }
    

Expected Behavior:

Both using System directives should be faded and candidates for removal. In fact removing both does not produce a compile error because of the implicit using.

Actual Behavior:

Only the directive outside the namespace is seen as redundant: image

This impacts commands like Run Code Cleanup on the solution. In a solution where the coding style is to put usings inside the namespace, after enabling implicit usings and running Code Cleanup, the only usings removed are those in files with no namespace at all.

This also repros with the newer namespace statement syntax: image

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 29 (19 by maintainers)

Commits related to this issue

Most upvoted comments

Yeah those examples make sense, thanks. Definitely makes it trickier.

Here’s a trivial example with extension methods:

using System;

namespace MyNamespace
{
  using X;
  using Y;

  class B
  {
    static void Main()
    {
        Bar(x => x.Goo(), null); // Prints 1
    }

    static void Bar(Action<string> x, object y)
    {
        Console.WriteLine(1);
    }

    static void Bar(Action<int> x, string y)
    {
        Console.WriteLine(2);
    }
  }
}

namespace X
{
    public static class A
    {
        public static void Goo(this int x)
        {
        }

        public static void Goo(this string x)
        {
        }
    }
}

namespace Y
{
    public static class B
    {
        public static void Goo(this int x)
        {
        }
    }
}

If you remove either of the inner using X/Y; (Because you see they came in from a global namespace), this will change the meaning of the code due to extension method and lambda inference. This is true regardless of whether the outer global using binds to the same namespace as the inner one.

then the inner one should be safe to remove, because we know that exactly same using is already in scope together with any relevant types and extension methods.

I don’t think you’re understanding the example i gave:

using System;

namespace A
{
    struct String { }
    namespace B
    {
        using System;

        public class C
        {
            static void Main(String s)
            {
            }
        }
    }
}

Here, both the inner and outer using System; resolve to the exact same namespace symbol. But removing the inner using will change the meaning of the code.

Again, this is not speculative concern. Many projects have code exactly like this.

Note that this issue is particularly annoying when using the new file-scoped namespaces. When using those, (at least to me) it feels more natural to put namespace ... at the very top of the file like this:

namespace A;

using System;

public class C
{
    static void Main()
    {
        String s = "Hi";
    }
}

But then those usings are considered internal to the namespace, which can be not obvious from a quick glance, and they don’t trigger the “unused using” rule.

Thanks for looking. I put the String use there deliberately, but both using directives are redundant because remember the project is configured with implicit usings. In the repro I can remove both and the code still compiles.

Yes, in this repro that may be the case. But consider the case of:

using System;

namespace A
{
    struct String { }
    namespace B
    {
        using System;

        public class C
        {
            static void Main()
            {
                String s = "Hi";
            }
        }
    }
}

In this case, it is not safe to remove the inner using System; If we do, then ‘String’ will now bind to A.String. This is a core problem in that there is a distinction between “this namespace is unused, and is therefor safe to remove” vs “this namespace is used, but code would mean the same thing if it were removed”.

In your example, the latter is true, but it’s certainly not universally true. And, it’s very complex to determine this. You effectively have rebind everything to see if it means something else. Recall as well that usings bring in namespace, types and extensions. How do you ensure that post-removal that every single piece of code means the same thing.

(As a bonus ‘hardness’ consider how extension methods may be used without being directly referenced. For example, an extension GetEnumerator() used in a foreach loop) 😦