Public methods returning a reference needs a contract to ensure that a non-null reference is returned
Code Contracts decrease caller/callee ambiguity; not ensuring a returned reference is non-null leaves ambiguity. Matches methods returning a reference type without using Contract.Ensure() (from Microsoft Code Contracts for .NET), typically: Contract.Ensures(Contract.Result<ReturnType>() != null, "returned reference is not null"); https://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970
How to fix
Use Microsoft Code Contracts for .NET on your API's public surface to remove null / not null ambiguities. For methods that might not return a result, prefer the TryXXX() pattern (like System.Int32.TryParse()) over returning null.
Estimated Debt: 5 minutes per method possibly returning null plus 3 minutes per caller.
Estimated Debt: 5 minutes per method possibly returning null plus 3 minutes per caller.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Public methods returning a reference needs a contract to ensure that a non-null reference is returned</Name>
// <Id>ND2900:PublicMethodsReturningAReferenceNeedsAContractToEnsureThatANonNullReferenceIsReturned</Id>
warnif count > 0
let ensureMethods = Application.Methods.WithFullName(
"System.Diagnostics.Contracts.__ContractsRuntime.Ensures(Boolean,String,String)")
from ensureMethod in ensureMethods
from m in ensureMethod.ParentAssembly.ChildMethods where
m.IsPubliclyVisible &&
!m.IsAbstract &&
m.ReturnType != null &&
// Identify that the return type is a reference type
(m.ReturnType.IsClass || m.ReturnType.IsInterface) &&
!m.IsUsing(ensureMethod) &&
// Don't match method not implemented yet!
!m.CreateA("System.NotImplementedException".AllowNoMatch())
select new {
m,
ReturnTypeReference = m.ReturnType,
Debt = (5+3*m.MethodsCallingMe.Count()).ToMinutes().ToDebt(),
Severity = Severity.Medium
}
//<Expl>
// The *public {0}* returns a reference and should be tagged
// with a contract to ensure that a non-null reference is returned
//</Expl>
//<Desc>
// **Code Contracts** decrease caller/callee ambiguity; not ensuring a
// returned reference is *non-null* leaves ambiguity. Matches methods
// returning a reference type without using **Contract.Ensure()** (from
// **Microsoft Code Contracts for .NET**), typically:
// *Contract.Ensures(Contract.Result<ReturnType>() != null,
// "returned reference is not null");*
// https://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970
//</Desc>
//<HowToFix>
// Use *Microsoft Code Contracts for .NET* on your API's public surface to
// remove *null / not null* ambiguities. For methods that might not return a
// result, prefer the **TryXXX()** pattern (like *System.Int32.TryParse()*)
// over returning *null*.
//
// Estimated Debt: 5 minutes per method possibly returning null plus 3
// minutes per caller.
//</HowToFix>