0

Is there any possibility to switch warnigs off on an individual, site-specific (not project-related) level?

Background: most warnings are null reference warnings on code that is already tested. In the heap of those, some mor important warnings may get lost.

I know that I can suppress warnings in the file and in the project.

Globally suppress c# compiler warnings

How to suppress Possible Null Reference warnings

Suppress warning on solution level. Treat warning as error on solution level (where the Answer on the question is from 2013 and it does not seem to work any more, and it would not solve my problem, either, which is:

As I am working in a team, I cannot suppress this in the projects because some other people may not like to suppress the warnings.

Is there any possibility to switch warnigs off on an individual, site-specific (not project-related) level?

SDG
  • 114
  • 6
  • 2
    "I get this warning even in evidently safe code" - well, yes, maybe. But do you *also* get it in situations where it's actually helpful? Also, how much will your coworkers on the team thank you for adding warnings for *them* that you were never aware of when you check in code? – Damien_The_Unbeliever Mar 30 '22 at 13:37
  • It would be helpful if there was more intelligence behind it. The way it works now, I don't see it in the heap of unuseful warnings. – SDG Mar 30 '22 at 13:44
  • 1
    You can always add a ! to things you know are not null - `item!.field` – Jay Buckman Mar 30 '22 at 14:19
  • What is the actual error? Is it complaining about `item` or `item.field`? – Jay Buckman Mar 30 '22 at 14:25
  • it's complaining about item. Just corrected an error in my code template; it is (item.field == "xyz"), not = – SDG Mar 30 '22 at 15:16
  • 1
    It's complaining because there is a situation where thing can go wrong. It's your job to correct it. (Do not ignore it, and leave it to your co-workers....) – Luuk Mar 30 '22 at 15:21
  • 2
    Can you give a [mcve] where this *actually* produces a warning "because the compiler is too stupid"? – Jon Skeet Mar 30 '22 at 15:21
  • 1
    If you have that many warnings that you can't see the "useful" ones, then maybe you should start fixing those warnings instead of just disabling them? But also, by suppressing that "at the user level" you are essentially saying "I'm gonna leave the fixing of the bugs I leave in my code to my teammates", which is at best rude and at worst totally unprofessional. – Etienne de Martel Mar 30 '22 at 15:22
  • 1
    In terms of what I mean: https://gist.github.com/jskeet/d1cc9ce87a85f6ddfe5e6d4ef822e7c4 does *not* produce a warning, although it does if you comment out the null check. My guess is that the warning isn't about what you think it is, but without providing a [mcve], we're stuck at guessing. I'd start by changing your approach of assuming that the compiler is "stupid". – Jon Skeet Mar 30 '22 at 15:24
  • @Jon Skeet I just made a Visual Studio version update. Now, surprise, I get no warning even if I omit the null check with the continue statement. So, I erase the example because the bug has been replaced... If anybody knows the answer to the actual question, welcome, else we forget it. – SDG Mar 31 '22 at 15:04
  • From what version was the update, (and to which version) ? (/me is just a bit curious) – Luuk Mar 31 '22 at 15:08
  • 17.0.1 to 17.1.2 – SDG Mar 31 '22 at 15:09
  • And the current setting of [Nullable](https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts) does not happen to be `disable` ? – Luuk Mar 31 '22 at 15:37
  • I would suggest just deleting the question if it's now obsolete. But please note that if you'd provided a [mcve] from the start (including a project file), we'd have been able to say with confidence whether or not *that exact example* displayed any warnings - which would very quickly have led to discovering that there were different versions of Visual Studio in play, I believe. – Jon Skeet Mar 31 '22 at 15:40
  • 2
    (And in general, I'd say the best approach is just to *fix the warnings*. Even if the code is already tested and not throwing any exceptions, fixing the warnings will usually make your code more self-describing, by accurately expressing whether or not it can accept/return null values.) – Jon Skeet Mar 31 '22 at 15:41
  • It's mainly code developed under C# 6 that only new nullable references and needs to be compiled like that. So, many fields and properties are never null if they are used in the methods but the compiler cannot know it. My question was actually not how to code but whether there is a possibility to switch it off. – SDG Apr 01 '22 at 06:34

1 Answers1

0

try do this way

if(collection is not null)
{
foreach(var item in collection)  
{  
   if(item.field = "xyz")
  {
    ...
  }
}
}
zort
  • 23
  • 4