Remove unused 'using' directives on GlobalUsings file

I have some unused  `Global using directives ` in my code.  `Code Cleanup` and  `Code | Optimize imports` cannot remove those directives. 

Also, I tested in class, that `Code Cleanup` and  `Code | Optimize imports` can remove the unused using directives. 

I don't want to delete those using directives by hand, is there any setting I need to change to remove those directives on GlobalUsings.cs?

 

JetBrains Rider 2024.1.5
Build #RD-241.18968.19, built on August 2, 2024
Runtime version: 17.0.11+1-b1207.30 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11.0
.NET Core v8.0.1 x64 (Server GC)

1
3 comments

It seems you confused the code style settings and code inspection in Rider. Code Cleanup will only apply code styles. Optimize Import will only optimize using directives added automatically.

Rider has the corresponding code inspection rule:

Since IDE usually does not add global using directives automatically, it is necessary to check all entries when deleting them. That's why Rider will only show the warning message to you but not change anything else. 

0

Code Cleanup will only apply code styles. Optimize Import will only optimize using directives added automatically.

Your answer does not match the actual situation I encountered.

If a class contains unused using directives, both Code Cleanup and Optimize Imports can remove these unused using directives, even those unused using directives were added manually.

So why can't unused using directives in GlobalUsings be removed?

0

Sorry for the misunderstanding. I'm not saying Optimize Import can only apply to “generated code”, but for the global using which needs to be optimized, the file is definitely modified manually.

The point is the “redundant global using directive” rule has only been triggered for manual-editing GlobalUsing.  

If you have <ImplicitUsings>enable</ImplicitUsings> set in your project, the GlobalUsing will be generated by MSbuild on build stage—in this situation, no hand editing is required. Thus the “redundant global using directive” rule will never trigger. 

Rider provides the ability to extract global usings from your source but still needs your confirmation. It's the same as if you are editing the file manually. IDE cannot delete the code needs to be double-checked, that's why the “redundant global using directive” is an inspection but not a code style checker.

0

Please sign in to leave a comment.