C
C#2y ago
Alexicon

❔ Suppress Visual Studio IDE0270 style rule without adding '.editorconfig'

I have some cases in my code where I have if statements like the following:
User? user = Users.FirstOrDefault(u => u.Id == id);

if (user is null)
{
throw new UserDoesNotExistException();
}
User? user = Users.FirstOrDefault(u => u.Id == id);

if (user is null)
{
throw new UserDoesNotExistException();
}
But the visual studio 'Error List' shows 'messages' that say 'Null check can be simplified' (fg. 1) which in some cases I am ok with simplifying but with the above example it would want to make it like this:
User user = Users.FirstOrDefault(u => u.Id == id) ?? throw new UserDoesNotExistException();
User user = Users.FirstOrDefault(u => u.Id == id) ?? throw new UserDoesNotExistException();
Which I personally don't like in this case. So how can I suppress this specific rule style code? I know you can configure code style within the code style section of the settings (fg. 2) but I have the 'perfer throw-expression' already set to 'no' so I don't know why this message still shows up. Obviously you can add a .editorconfig to the actual project but I do not want to do that. I want it as part of the permanent code style within my visual studio. Maybe this is just bugged? If you click the link in the error list for this error code 'IDE0270' it takes you to a 404... (https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0270).
fg. 1
fg. 2
5 Replies
whiteviperx
whiteviperx2y ago
// Disable IDE0270 I think that should work
Alexicon
AlexiconOP2y ago
@whiteviperx Maybe but like I said I want to make it permanent since I am working in several solutions and I don't want to modify the repositories. All other code styles I have managed to configure but this one.
whiteviperx
whiteviperx2y ago
An odd idea might be to add it the the default header?? if you get a chance can you look at my question? You could also try this ... create the editorconfig file and put it in the folder C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE.... or similar??? just a thought
Alexicon
AlexiconOP2y ago
@whiteviperx that might work. I am away right now but will try that when I get a chance
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server