Possible 'null' assignment to entity with '[NotNull]' attribute reported on CallerMemberName parameter
I have a parameter marked with CallerMemberName attribute and when I use that parameter to call GetType().GetMethod() method Resharper reports Possible 'null' assignment to entity with '[NotNull]' attribute warning. I guess this is because the parameter has default value of null as required by C# compiler but it's also guaranteed not to be null so Resharper shouldn't be reporting this warning.
ReSharper 2016.3.2
Visual Studio 14.0.25431.1.
Please sign in to leave a comment.
But it's not.
It you call the method without passing the parameter, it will always have a value. But there's nothing to prevent you passing an explicit value of null to the parameter.
Try the following in LINQPad:
The output from the three calls will be: "Main", "Me", and null.
OK, Thanks. I don't know why would anyone do it but I understand. By the way does Resharper show a warning in the third case at the caller side? Also is it possible for Resharper to analyze callers and if there is no caller which passes null don't show the warning it shows?
There's a configurable inspection for "Explicit value passed to parameter with caller info attribute", which would flag the second and third case.
You could also try using the Code Annotations attributes to add [NotNull] to the caller parameter, which would flag the third case with a warning, and remove the warning from within the method.