Weird code cleanup/code style with using statements and preprocessor directives

Hello there,

I've encountered a weird issue with the code cleanup feature. I'm not sure whether it's a bug or if I just configured it the wrong way, but this is what happens (I'm using Rider 19.1.3 with Unity btw.):

For example, take this code:

Now I do my code cleanup with the specified code style and after that it looks like this:

This obviously leads to issues, because it completely rearranged the using-statements so some of them are now INSIDE of the the if-preprocessor-directives even though the weren't before and I have no idea why it does that. 

Does anyone know what setting I need to adjust to correct this?

Thank you very much in advance!

4 comments
Comment actions Permalink

Hi Christian! 

Could you please try to update Rider and recheck it? I can't reproduce this on 2019.2.2. 

If the problem persists, could you add a small code snippet where you can reproduce the issue? Thank you in advance.

0
Comment actions Permalink

Hi, 

 

okay, this is really strange. I tried to reproduce the issue with an "empty" new Unity project and added a new class.

After that I added these using-statements just for the purpose of testing it:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.Serialization;

#if UNITY_EDITOR
using UnityEditor;
#if UNITY_2018_3_OR_NEWER
using UnityEditor.Experimental.SceneManagement;
#endif
#endif

public class NewBehaviourScript : MonoBehaviour
{
[FormerlySerializedAs("test")] private String teststring;
private List<bool> testlist = new List<bool>();

[EditorBrowsable(EditorBrowsableState.Never)]
private void Test()
{
foreach (var VARIABLE in testlist.Where(b => b))
{
// ...
}

if (PrefabStageUtility.GetCurrentPrefabStage() != null)
{
}

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
}
}

// The code existing is obviously only around so Rider knows it needs the using statements and doesn't remove them completely.

// Now these same using-statements in a framework I'm using (that only uses Unity) rearranges the using-statements from this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.Serialization;

#if UNITY_EDITOR
using UnityEditor;
#if UNITY_2018_3_OR_NEWER
using UnityEditor.Experimental.SceneManagement;
#endif
#endif

// to this...

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Cryptography;
using UnityEditor;
#if UNITY_2018_3_OR_NEWER
using UnityEditor.Experimental.SceneManagement;
using UnityEngine;
using UnityEngine.Serialization;
#endif
#endif

Which may be what Rider thinks would be correct, but actually alters the code in a way, that building the project from within Unity leads to errors.

The problem DOES NOT persist in 19.2.2. In Rider 19.2.2 it works like it's supposed to.

Thanks in advance.
Chris

0
Comment actions Permalink

Hi Chris, 

The best way is to timely update to the latest version if you have such a possibility (not a fall-back licence). 

However, you can disable optimising usings even in full cleanup profile. Go to `File | Settings | Editor | Code Cleanup`, clone the Full cleanup profile and then uncheck the `Optimize 'using' directives` section. Use this new profile instead of the default "Full Cleanup".  For more information, please follow this article.

Hope this helps! 

0

Please sign in to leave a comment.