Search code examples
.netcode-contracts

What does the "Be optimistic on external API" option do?


In the "Static Checking" options for Code Contracts there is an option named "Be optimistic on external API". I cannot find any documentation on what this option does. How does it affect the analyzer's behaviour?


Solution

  • There does indeed not seem to be any documentation about this option. Browsing the source gives a few clues. In Options.cs this links to a boolean lowScoreForExternal:

    [OptionDescription("Be optimistic on external API? 
                        We will assign proof obligations depending on that a low score")]
    [DoNotHashInCache]
    public bool lowScoreForExternal = true; 
    

    Where in the WarningScoresManager this is used during initialization to set the score assigned for extra info in an external assembly. With the option turned on, the same score is applied if referencing a different assembly as is applied for a framework assembly, otherwise a very high penalty applies.

    private void InitializeDefaultValuesForContextsOfCalleeAssumeCanDischarge
      (bool lowScoreForExternalAPI)
    {
       // ...
    ScoreCalleeAssumeExtraInfoDeclaredInAFrameworkAssembly = .05;
    ScoreCalleeAssumeExtraInfoDeclaredInADifferentAssembly = lowScoreForExternalAPI ?
            ScoreCalleeAssumeExtraInfoDeclaredInAFrameworkAssembly : 100.0;
    

    It would seem that using this setting forces the analyzer to assume a similar level of trust about the code quality of external assemblies as is afforded to framework assemblies.

    This seems to be the only use of this variable - it is passed to the WarningScoresManager in the constructor and is not used anywhere else.