Custom highlight policy.
Good day to all.
I'm sorry, if this question was asked before. It means, that I simply could not
find an answer. In this case It would be enought to get the link =)
I am trying to find out, how to add custom policy to code inspection. Valid example:
Possible null exception highlight is disabled if there is System.Debug.Assert, that ensures, that object is not null.
Or it offers to add assertion automatically. I do not have possibility to use default assertions, as it is now allowed
in this framework(mono in context of Unity3d), so I use some custom equivalent. So, I want to add custom policy,
that would grant the same behaviour as with described above, but using my custom assertion instead of default.
Thanks at least for reading =)
As I'd said before, I would be glad even to get link to the answer if it is already exist.
P.S. I'd already checked official documents here http://www.jetbrains.com/resharper/webhelp/Code_Analysis__Configuring_Warnings.html
but doesn't understand, how to realise what I want =\ Maybe I just need an example...
Please sign in to leave a comment.
Try decorating your custom assertion method with the ContractAnnotationAttribute:
This used to work for Resharper, but the latest Rider does respect this as far as I can see. It is a bug? Are there any workarounds?
Hello Lauri Lubi,
Could you share a sample solution illustrating the issue? You may attach it to a support ticket created by the "Submit a Request" button above.
Hi, while re-creating a demo example, I noticed that the key was C# nullability context. Also known as
in C# project-file. I tested with C# language version 8.0.
Example:
Here is a screenshot of the warning.
I get the same warning for a nullable parameter.
Hello Lauri Lubi
The warning comes not from ReShrper's analysis but from Roslyn, it's CS8602 compiler warning. So maybe it's worth reporting to Microsoft if you believe it's incorrect.
Oh, I see! Before I can report it to MS... Resharper analysis reads the [ContractAnnotation] on method Assert() and therefore knows that args cannot be null after that. Do you think there is a similar attribute for Roslyn?
Lauri Lubi
There is the [NotNull] attribute as far as I know
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis
It looks like the [DoesNotReturnIf(false)] attribute for the condition parameter should work out in this case.
Thank you!
[DoesNotReturnIf(false)] does the trick! It seems like both Roslyn and Rider/Resharper respect it, so [ContractAnnotation] is not needed any more.