Custom highlight policy.

Answered

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...

9 comments
Comment actions Permalink

Try decorating your custom assertion method with the ContractAnnotationAttribute:

[ContractAnnotation("halt <= condition: false")]
public void Assert(bool condition, string text)

0
Comment actions Permalink

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?

0
Comment actions Permalink

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.

0
Comment actions Permalink

Hi, while re-creating a demo example, I noticed that the key was C# nullability context. Also known as

<Nullable>enable</Nullable>

in C# project-file. I tested with C# language version 8.0.

Example:

using System;
using System.Diagnostics;
using JetBrains.Annotations;

namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
AssertTools.Assert(args != null, "Bad input");

Console.WriteLine($"Hello World! {args.Length}");
}
}

[DebuggerStepThrough]
public static class AssertTools
{
[ContractAnnotation("halt <= condition: false")]
public static void Assert(bool condition, string? message = null)
{
if (condition) return;
throw new Exception(message ?? string.Empty);
}
}
}

Here is a screenshot of the warning.

 

I get the same warning for a nullable parameter.

static void Main(string[]? args)

 

0
Comment actions Permalink

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.

0
Comment actions Permalink

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?

0
Comment actions Permalink

It looks like the [DoesNotReturnIf(false)] attribute for the condition parameter should work out in this case.

using System;
using System.Diagnostics;
//using JetBrains.Annotations;

namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
AssertTools.Assert(args != null, "Bad input");

Console.WriteLine($"Hello World! {args.Length}");
}
}

[DebuggerStepThrough]
public static class AssertTools
{
//[ContractAnnotation("halt <= condition: false")]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIf(false)] bool condition, string? message = null)
{
if (condition) return;
throw new Exception(message ?? string.Empty);
}
}
}
0
Comment actions Permalink

Thank you!

[DoesNotReturnIf(false)] does the trick! It seems like both Roslyn and Rider/Resharper respect it, so [ContractAnnotation] is not needed any more.

 

0

Please sign in to leave a comment.