Proposal: #pragma warning disable once #1070
Replies: 18 comments 17 replies
-
Conceptually I'm not against the idea of simplifying this case. However, the proposed syntax leaves a lot of questions for me (i.e. it might not be intuitive and/or there could be a better way to approach this):
|
Beta Was this translation helpful? Give feedback.
-
Ya know, I don't think I've ever used |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Three most common cases:
Also one project I worked on looked at the "most recently changed date" for methods and applied different testing requirements, so changes to old code could result in unexpected developer overhead to address secondary policies (in this case, a global suppressions file was used to target the warning locations). |
Beta Was this translation helpful? Give feedback.
-
Ah, I see, so something that's more commonly triggered via analyzers where you might be applying stricter and more pedantic rule sets that you might want to shut-up on an individual basic, just like Resharper. That does make sense. When dealing with vanilla C# compiler warnings I don't think I've ever encountered a situation where the compiler couldn't be satiated with what is often better/shorter code anyway. 95% of the time it seems to be the result of the fact that other developers don't seem to realize that you can omit the variable name in a |
Beta Was this translation helpful? Give feedback.
-
If that line has a single statement, all instances are suppressed; the directive applies to the whole statement.
Nothing. The directive applies to the next statement, not to the next occurrence of the warning.
Blank lines would have no effect. Only statements should be considered. The directive would apply to the first statement following it.
That's a tricky one... I'm not sure what it should do in this case.
As I said, lines don't matter, only statements do.
They would all apply to the next statement. |
Beta Was this translation helpful? Give feedback.
-
In addition to the scenarios mentioned by @sharwell: |
Beta Was this translation helpful? Give feedback.
-
Not to mention the three or four I typically put in for pinvoke structs. |
Beta Was this translation helpful? Give feedback.
-
I had to use this a number of times recently before support for generic patterns was added in 7.1. Ultimately though I tend to use the suppression file approach. As for scope, I think it would be better to say something like # pragma warning disable next line FOO123 Which is used by JavaScript and TypeScript tools such as eslint and tslint. As for multi line verbatim string-literals, I'm not sure if the same approach could be used but such tools don't have issues with ECMAScript multi-line strings so perhaps a similar approach could be used. Just throwing ideas out there. |
Beta Was this translation helpful? Give feedback.
-
Another use case is when the framework or a library will call certain optional methods through reflection. One example is the mstest ClassInitialize method which require a TestContext argument. When you don't use that argument you get IDE0060. I expect there might be similar cases for asp.net global asax things and/or console host args etc. I guess interfaces for these things would remove this problem, but as it stands this pops up. |
Beta Was this translation helpful? Give feedback.
-
Isn't the problems described above already resolved for C++ using #pragma warning supress? What I mean, is that they've already worked through all the edge cases, and this was their final conclusion, and most likely well reasoned and battle tested. We should adopt the same pattern for C# for the same reasons they did. imho. Below are examples of how it works in C++, and I see no practical reason we shouldn't follow the same convention? It's very readable, and the concept of push and pop solves problems of really bad bugs creeping in by having a piece of code inadvertently creating a coupling (changing behavior) of other code using pragmas. example of supress warning 1234 in C++ #pragma warning (disable : 4507 34; once : 4385; error : 164; supress : 1234 ) C++ supress docs here important point is this text
|
Beta Was this translation helpful? Give feedback.
-
if we are going to deviate from the C++ convention, then I'd propose that example #pragma warming (disable: 1234)
using System;
namespace Foo {
public class Bar ...
} |
Beta Was this translation helpful? Give feedback.
-
Any news on this? |
Beta Was this translation helpful? Give feedback.
-
From my dupe issue: it would be nice to also have (in addition to syntax shown above) the ability to suppress warnings like you find in other tools/languages, where the suppression is inline. That reduces noise to a minimum. Something like: SomethingThatTriggersX(); #pragma warning disable-line X |
Beta Was this translation helpful? Give feedback.
-
And there's another question: what is a line? Multi-line statement isn't that rare. The associated span of the warning can also cross multiple lines. Disabling the next warning is also a bad idea. If the closed warning disappears and another warning happens, the new one can be eaten. |
Beta Was this translation helpful? Give feedback.
-
That may be true, and unfortunately there are other problems noted above by sharwell as well. So instead of reinventing the wheel, maybe the best idea is to examine how javascript / typescript tooling does it. |
Beta Was this translation helpful? Give feedback.
-
I'm an enthusiastic fan of Static Analysis tooling and I universally use inline SuppressMessage attributes, not
Do we need to improve |
Beta Was this translation helpful? Give feedback.
-
This is a great idea! I raised a separate issue as I wasn't aware of the discussion here. dotnet/roslyn#60668 There were 2 separate parts to my request:
Single line ignoreRather than: #pragma warning disable IDE0052
private readonly ILogger<WeatherForecastController> _logger;
#pragma warning restore IDE0052 What if instead, something like this existed: #pragma warning disable-line IDE0052
private readonly ILogger<WeatherForecastController> _logger; Lightbulb assistance in VS CodeIn ESLint you get lightbulb assistance in VS Code to implement an ignore for you; see the "Disable no-var for this line" below: Imagine if the lightbulb with the C# extension offered the option to ignore a lint; just as the ESLint menu does. Alas at the moment, it doesn't: The above lightbulb is for the following issue:
If it did offer this it could be quite a productivity boost |
Beta Was this translation helpful? Give feedback.
-
I like the proposed
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently when we need to disable a warning for a given statement, we have to enclose it between
#pragma warning disable
and#pragma warning restore
directives:This is quite verbose and impedes code readability.
Proposal: introduce
#pragma warning disable once
directive that disables a warning for the following statement only:The idea is borrowed from ReSharper's warning suppression comments:
Beta Was this translation helpful? Give feedback.
All reactions