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

[VS2015] ccrewrite fails with CC1004: Malformed contract. Found Requires after assignment in method #221

Closed
DzonnyDZ opened this issue Aug 23, 2015 · 4 comments

Comments

@DzonnyDZ
Copy link

Note: This situation is different from #133.
I tried the fixed ccrewrite mentioned in #133
, but it didn't help me. I'm still getting the error on code like this.

public void MoveFilesToAccessibleStorage(string ugcTempFolderLocal, string ugcTempFolderRemote, string appDrive)
        {
            Contract.Requires<ArgumentNullException>(ugcTempFolderLocal != null, "ugcTempFolder");
            Contract.Requires<ArgumentException>(ugcTempFolderLocal != string.Empty);
            Contract.Requires<ArgumentException>(!ugcTempFolderLocal.Contains(IO.Path.GetInvalidPathChars()));
            Contract.Requires<ArgumentException>(!ugcTempFolderLocal.Contains(":"));
            Contract.Requires<ArgumentException>(!ugcTempFolderLocal.EndsWith("\\") && !ugcTempFolderLocal.EndsWith("/"), "Do not provide trailing slashes for folder names");
            Contract.Requires<ArgumentException>(!ugcTempFolderLocal.StartsWith("\\", "/") || ugcTempFolderLocal.StartsWith("\\\\", "//", "\\/", "/\\"));
            Contract.Requires<ArgumentNullException>(appDrive != null, "appDrive");
            FileSystemHelper.ContractRequireRelativeFolderName(ugcTempFolderRemote);
            Contract.Requires<ArgumentException>(appDrive.Like("[A-Za-z]:\\"));
        }

on the line

Contract.Requires<ArgumentException>(!ugcTempFolderLocal.StartsWith("\\", "/") || ugcTempFolderLocal.StartsWith("\\\\", "//", "\\/", "/\\"));

20>C:\Pangea\common\RFERL.IBll\Backend\IAudioOperations.cs(116,13): error CC1004: Malformed contract. Found Requires after assignment in method 'RFERL.Bll.Backend.IAudioOperationsContract.MoveFilesToAccessibleStorage(System.String,System.String,System.String)'.

I'm creating this issue to be able to trace it separately.

@DzonnyDZ
Copy link
Author

It looks like that CodeContracts has problem with VB method like this:

   ''' <summary>Test if a string starts with any of given substrings</summary>
        ''' <param name="str">A string to test</param>
        ''' <param name="substrings">Substrings to look for at the beginning of <paramref name="str"/></param>
        ''' <returns>True if <paramref name="str"/> starts with any of <paramref name="substrings"/>. If <paramref name="str"/> is null returns true only if <paramref name="substrings"/> contains null. If <paramref name="substrings"/> is null returns false.</returns>
        ''' <remarks>This overload uses current culture and performs case-sensitive comparison</remarks>
        ''' <seealso cref="String.EndsWith"/>
        <Extension()>
        Public Function StartsWith(str As String, ParamArray substrings As String()) As Boolean
            Return StartsWith(str, StringComparison.CurrentCulture, substrings)
        End Function

        ''' <summary>Test if a string starts with any of given substrings (allows to specify comparison rules)</summary>
        ''' <param name="str">A string to test</param>
        ''' <param name="comparison">Defines which rules use to compare strings</param>
        ''' <param name="substrings">Substrings to look for at the beginning of <paramref name="str"/></param>
        ''' <returns>True if <paramref name="str"/> starts with any of <paramref name="substrings"/>. If <paramref name="str"/> is null returns true only if <paramref name="substrings"/> contains null. If <paramref name="substrings"/> is null returns false.</returns>
        ''' <remarks>This overload uses current culture and performs case-sensitive comparison</remarks>
        ''' <seealso cref="String.EndsWith"/>
        <Extension()>
        Public Function StartsWith(str As String, comparison As StringComparison, ParamArray substrings As String()) As Boolean
            If substrings Is Nothing Then Return False
            If str Is Nothing Then Return substrings.Contains(CStr(Nothing))
            Return substrings.Any(Function(substring) str.StartsWith(substring, comparison))
        End Function

The 1st overload (which calls the 2nd one)

@DzonnyDZ DzonnyDZ changed the title [VS20156] ccrewrite fails with CC1004: Malformed contract. Found Requires after assignment in method [VS2015] ccrewrite fails with CC1004: Malformed contract. Found Requires after assignment in method Oct 1, 2015
@YoleyTeichman
Copy link

Any updates about this issue? I'm facing the same error at this line

Contract.Ensures(type.IsStubGenericArgument() || Contract.Result<string>().Contains(type.Name.Substring(0, type.Name.IndexOfAny(new char[] { '`', '+' }) >= 0 ? type.Name.IndexOfAny(new char[] { '`', '+' }) : type.Name.Length)));

@hubuk
Copy link
Contributor

hubuk commented Feb 24, 2016

Simplified scenarios:

// both trigger malformed contract error
Contract.Requires(new object[] { string.Empty } != null);
Contract.Requires(new object[] { 0 } != null);
// this is OK
Contract.Requires(new object[] { null } != null);

@hubuk
Copy link
Contributor

hubuk commented Feb 24, 2016

Should be fixed in #390.

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

No branches or pull requests

4 participants