Stop ReSharper from Adding Annotations
Asked Answered
I

1

16

I'm using ReSharper in my C# projects, and generally I love it. However, it keeps adding annotations to the code when I do certain refactoring actions.

For example, it adds [NotNull] when I use the "Check parameter for null" context action:

// Before context action
public void Foo(object input)
{
}

// After context action
public void Foo([NotNull] object input)
{
    if (input == null)
    {
        throw new ArgumentNullException("input");
    }
}

Additionally, ReSharper then adds using JetBrains.Annotations to the file, even though I'm not referencing the JetBrains assembly where the attribute is defined.

I would like to continue using the context actions, as they're very useful, but I can't introduce external annotations in the code. ReSharper provides an option to change the default annotation namespace and to copy the annotation attribute source code into your project, but that also isn't an option for this project.

Is there any way to tell ReSharper to stop adding annotations entirely? I've tried in the options unchecking JetBrains.Annotations as a namespace with code annotation attributes, but that doesn't seem to have any effect on whether the annotations are generated in the first place.

Update: While I wasn't actually referencing JetBrains.Annotations.dll, I was referencing another DLL that re-implemented the same annotation attributes in the same JetBrains.Annotations namespace. Removing that DLL reference will prevent ReSharper from adding the annotations. It'd be nice if there was still an option to turn this off, but the workaround fits for this situation.

Izzard answered 18/3, 2013 at 19:34 Comment(2)
NLog was the DLL causing me problems. I can't remove it so I've resorted to adding the Annotations to our root namespace because living without ReSharper isn't really living at all.Irony
Strongly advise the usage of annotations.Materiel
S
6

When you reference the JetBrains.Annotations.dll, the default action for "Check parameter for null" seems to be to use the NotNull attribute (despite adding an "Annotate with 'NotNullAttribute'" option.

The only workaround that I know is to not reference JetBrains.Annotations.dll.

Update:

looking more into this, it seems that there was a suggestion/bug that [NotNull] be added when the annotations dll is included and "Check parameter for null" is requested: http://youtrack.jetbrains.com/issue/RSRP-70350 In case anyone else happens across this and wonders why...

Sericeous answered 18/3, 2013 at 22:53 Comment(1)
thanks! I wasn't referencing JetBrains.Annotations.dll, but I was referencing another DLL which had re-defined all the same attributes in the same namespace. Removing that causes ReSharper to stop adding the attribute.Izzard

© 2022 - 2024 — McMap. All rights reserved.