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:
- Create a C# 10 console application with
<ImplicitUsings>enable</ImplicitUsings>in the project file. - Populate the
Program.csfile 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:

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:

About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 29 (19 by maintainers)
Yeah those examples make sense, thanks. Definitely makes it trickier.
Here’s a trivial example with extension methods:
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.I don’t think you’re understanding the example i gave:
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:But then those
usings are considered internal to thenamespace, which can be not obvious from a quick glance, and they don’t trigger the “unused using” rule.Yes, in this repro that may be the case. But consider the case of:
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) 😦