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

SA1217 asks for incorrect sorting with System usings #2163

Closed
RobinHood70 opened this issue Jul 2, 2016 · 4 comments
Closed

SA1217 asks for incorrect sorting with System usings #2163

RobinHood70 opened this issue Jul 2, 2016 · 4 comments
Assignees
Milestone

Comments

@RobinHood70
Copy link

RobinHood70 commented Jul 2, 2016

This was discussed in pull #786, but it doesn't seem to have been addressed.

If you ask VS to sort usings (Edit, Intellisense, Organize Usings), assuming you have the option set in your preferences, it will sort them with "using static System.*" on top of other "using static"s. StyleCop, however, is asking for that to be alphabetical.

@dlemstra
Copy link
Member

@RobinHood70 Do you have an example so we could add unit test for this and try to fix it?

@RobinHood70
Copy link
Author

In one of my projects, I have the following mess of usings:

using System;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Configuration;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using Design;
using WikiCommon;
using static System.Net.HttpStatusCode;
using static ClientShared;
using static Properties.Messages;
using static WikiCommon.Globals;

If you do a Remove and Sort Usings, this is the order they appear in. However, SA1217 triggers in the System.Net.HttpStatusCode because it's not in alphabetical order. This behaviour differs from the regular usings rule, which allows System usings to be sorted first (as they are above).

@commonsensesoftware
Copy link

Looks like this is still up for someone to update. I've also noticed that this rule (SA1217) conflicts with SA1208 in some cases. Visual Studio has a setting to do System first or alphabetical. This rule can also be realized in .editorconfig with the dotnet_sort_system_directives_first = true setting. However, there is no setting to sort using one way and using static another way. This means that the two rules conflict unless all using statements are sorted alphabetically.

Personally, I find myself using the Remove and Sort Usings (CTRL+R, CTRL+G) shortcut a lot now, but I always end up having to manually fix the using static because the SA1217 says they can only be alphabetic. While I prefer the simplicity of alphabetic only, I work with teams where that's not always the established practice. I think the rules should be congruent; one way or the other.

System First

dotnet_sort_system_directives_first = true
using System;
using System.IO;
using Microsoft.Security;
using static System.Math;
using static Microsoft.Security.IdentityType;

All Alphabetic

dotnet_sort_system_directives_first = false
using Microsoft.Security;
using System;
using System.IO;
using static Microsoft.Security.IdentityType;
using static System.Math;

@lostindark
Copy link

Ran into the same issue as well. Given VS 2017 can automatically do code clean up upon save (sort using), this will cause more issues. The fix should be prioritized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants