Fody / NullGuard

Adds null argument checks to an assembly

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Code Analysis error CA1065 when overriding ToString

DanielBretzigheimer opened this issue · comments

Description
If you override a ToString() method the visual studio shows me the warning CA1065 "Do not raise exceptions in unexpected locations" when running the code analysis (MSDN: https://msdn.microsoft.com/library/bb386039.aspx)

Expected Behaviour
NullGuard should at least have the option to ignore all ToString() methods.

Current Workaround
Setting the [return: NullGuard.AllowNull] on the ToString() resolves the warning

At the time of writing, the docs don't say something about the return nullability of ToString(), but in MS Code Contracts they define the result as not null. I think the not-null contract should outweigh CA1065 in this case and therefore would propose to inject a SuppressMessage attribute for CA1065 instead.

The silly thing about this is that the ToString() method is never null but it shows me the warning (because the exception CAN be thrown). So this means all ToString() methods would show this warning.

Example Code:

public class TestClass
{
    public string Something { get; set; } = "Something";

    public override string ToString()
    {
        return $"{this.Something}";
    }
}

If you run the code analysis over this code you get the warning. Even the default value for the property does not help to hide it.

So setting the attribute would work but it would be a huge effort for us. So do you think there is the possibility about implementing a ignore for this case?

@DanielBretzigheimer I meant to add an option that NullGuard injects a SuppressMessage attribute for CA1065. In my eyes this would be better than disabling the null return value check because the contract of ToString() is non nullable (for reference see above).

commented

In my opinion, this should be changed. Even a ToString overload like that raises a CA1065 warning. There is no CA1065 warning without NullGuard.

public override string ToString() { return "Test"; }

It's not only ToString(), it's any method listed in the MSDN linked by @DanielBretzigheimer. In my case it was implicit cast operators that led me here

    public static implicit operator TestClass(string id)
    {
        return new TestClass { Something = id };
    }

    public static implicit operator string(TestClass tenant)
    {
        return tenant.Something;
    }

Both raise CA1065 for both ArgumentNullException and InvalidOperationException (NullGuard checks on the way in and out). Suppressing the warning doesn't work for me, I still get the warning regardless of whether I add a SuppressMessage to GlobalSuppressions, or inline with the class.

If that could be made to work, then I would second @ulrichb - if NullGuard could inject the SuppressMessage attribute for methods it knows will fall foul of this warning I think that would be the correct solution.

yeah CA1065 is wrong. closing this