4

So I used to have Debug.Assert all around my code but now I have created a DebugHelper.Assert which in itself has some logging and a Debug.Assert but now I'm getting 1 million of warnings that something can be null.

Can I tell ReSharper that doing DebugHelper.Assert is the same as Debug.Assert and thus get rid of the warnings?

EDIT: I would really like (also maybe have to) avoid adding another dependency to the project just for this.

ditoslav
  • 4,563
  • 10
  • 47
  • 79
  • http://stackoverflow.com/questions/8854701/disable-all-resharper-warnings-with-a-comment – Nikhil Agrawal Feb 24 '17 at 10:29
  • 2
    @NikhilAgrawal That simply disables all warnings, not what's being asked here – Rob Feb 24 '17 at 10:30
  • @Rob: I know, but something to think on these lines. Instead of giving a pre baked answer to OP, I directed him to think on these line, informing that this 'kind' of functionalities do exist in Resharper. I have link to exact answer which he wants to find. but why give a Man a Fish.... – Nikhil Agrawal Feb 24 '17 at 10:36

1 Answers1

3

You want to add an attribute to the DebugHelper.Assert method. You can reference the JetBrains.Annotations package in your project, and add a Contract assertion, something like:

[ContractAnnotation("condition:false => halt")]
public void Assert(bool condition)
{
  // ...
}

This tells ReSharper that if the condition parameter is false, the method will halt. This is how ReSharper annotates the standard Debug.Assert method (albeit via "external" annotations in an xml file, rather than inline in the code).

You can also mark the JetBrains.Annotations package as a development time dependency - the attributes are conditional, and not compiled into the resulting binary by default. More details in this blog post.

citizenmatt
  • 18,085
  • 5
  • 55
  • 60
  • 1
    This is a nice solution, however I'm not sold on creating a dependency on Jetbrains' library (even a development dependency). Do you know if it's possible to do the same with resharper configuration files - I suppose, similar to how they annotate `Debug.Assert`? – Rob Feb 24 '17 at 11:42
  • +1 to @Rob . I'm really not liking this idea of adding a dependency and I'm not sure whether I will be allowed to even use it as a dev dependency because of my companies policies where I may need additional approval. I would really like to avoid adding this – ditoslav Feb 24 '17 at 11:55
  • Could this somehow be used https://www.jetbrains.com/help/resharper/2016.3/Code_Analysis__External_Annotations.html ? EDIT: Looks to me like not. It seems these can't be applied back to the same project – ditoslav Feb 24 '17 at 11:58
  • I can't accept this as an answer. I need something that doesn't add an extra dependency to my solution – ditoslav Feb 24 '17 at 13:44
  • If you add a nuget reference, it doesn't get compiled into the project, as all of the attributes are conditional. This means that although you have a reference, you don't need to distribute the library. Alternatively, and also documented in the blog post, you can add the source for the annotations to your project. These will be `internal` classes, so they don't become part of your public API, and because they're conditional, they don't even get compiled into the resulting binary. – citizenmatt Feb 25 '17 at 11:08