gpt4 book ai didi

cql - CQL 可以用来查找返回 `null` 的方法吗?

转载 作者:行者123 更新时间:2023-12-03 03:34:38 27 4
gpt4 key购买 nike

我想找到所有可以显式返回 null 的方法。

在 NDepend 中使用 CQL 这可能吗?

最佳答案

目前还不行,CQL 到目前为止还不知道变量、字段和返回值的值。

但是,建议使用以下默认规则。这个想法是,如果一个方法返回一个引用,它永远不应该为空,并且应该添加一个契约来断言这一点。如果您希望此类方法返回 null,请改用 Try... 模式,如 TryParse(string s, out T val):bool

// <Name>Public methods returning a reference needs a contract to ensure that a non-null reference is returned</Name>
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
}

//<Description>
// **Code Contracts** are useful to decrease ambiguity between callers and callees.
// Not ensuring that a reference returned by a method is *non-null* leaves ambiguity
// for the caller. This rule matches methods returning an instance of a reference type
// (class or interface) that don't use a **Contract.Ensure()** method.
//
// *Contract.Ensure()* is defined in the **Microsoft Code Contracts for .NET**
// library, and is typically used to write a code contract on returned reference:
// *Contract.Ensures(Contract.Result<ReturnType>() != null, "returned reference is not null");*
// https://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970
//</Description>

//<HowToFix>
// Use *Microsoft Code Contracts for .NET* on the public surface of your API,
// to remove most ambiguity presented to your client. Most of such ambiguities
// are about *null* or *not null* references.
//
// Don't use *null* reference if you need to express that a method might not
// return a result. Use instead the **TryXXX()** pattern exposed for example
// in the *System.Int32.TryParse()* method.
//</HowToFix>

关于cql - CQL 可以用来查找返回 `null` 的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31478786/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com