Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Pre and Post Conditions are suggested for "value != null" even when there is already a condition for !string.IsNullOrWhiteSpace(value) #437

Open
jmckone3 opened this issue Jun 11, 2016 · 9 comments

Comments

@jmckone3
Copy link

While this doesn't create incorrect behavior, it does create a lot of noise and make the true warnings much less valuable.

@SergeyTeplyakov
Copy link
Contributor

Thanks a lot.

Could you provide a version of Code Contract that you're using?

@SergeyTeplyakov
Copy link
Contributor

I've tried with string.IsNullOrEmpty and got the same result. Could you please check with !string.IsNullOrEmpty as well? This can simplify investigation...

@jmckone3
Copy link
Author

I am using the latest RC - DotNet.Contracts 1.10.20606.1

Yes. I also get the same issue with string.IsNullOrEmpy()

@yaakov-h
Copy link
Contributor

What version of .NET Framework are you targetting?

This smells like #339 to me.

@jmckone3
Copy link
Author

.Net Framework is 4.5.1.1

@yaakov-h
Copy link
Contributor

How did you get a .NET 4.5.1.1 target framework? As far as I know, that doesn't exist.

@KAW24
Copy link

KAW24 commented Jun 21, 2016

I can reproduce this using String.IsNullOrEmpty as follows:

public class Test
{
    private readonly string m_name;

    public Test(string name)
        : base()
    {
        Contract.Requires(!string.IsNullOrEmpty(name));
        m_name = name;
    }

    public string Name
    {
        get
        {
            Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()));
            // warning : CodeContracts: Consider adding the postcondition
            // Contract.Ensures(Contract.Result<System.String>() != null); to provide extra-
            // documentation to the library clients
            return m_name;
        }
    }

    [ContractInvariantMethod]
    private void ObjectInvariant() => Contract.Invariant(!string.IsNullOrEmpty(m_name));
}

I'm not sure this is related to #339 since there is no mention of [Pure] in the warning.

This was tested with VS 2015 Update 2 and Code Contracts 1.9.10714.2. The warning occurs for me when targeting both 4.6 and 4.6.1.

@SergeyTeplyakov
Copy link
Contributor

It seems that cccheck can't infer relationship between nullness of the variable from the predicate !string.IsNullOrEmpty(s)...

Valid bug and it seems that this is not a regression. We'll see what we can do.

Up for grabs:)

@jmckone3
Copy link
Author

Was using .NET Fx 4.5.1
Sorry about the typo.

Thanks for confirming the issue Sergey.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants