-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathVariableNameTooShort.ql
More file actions
59 lines (54 loc) · 1.49 KB
/
VariableNameTooShort.ql
File metadata and controls
59 lines (54 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @name Variable name is too short
* @description Variables should have meaningful names so that code is comprehensible. This query finds variable names that are too short.
* @kind problem
* @problem.severity recommendation
* @precision low
* @id cs/short-variable-name
* @tags maintainability
*/
import csharp
predicate sourceFile(File f)
{
f.fromSource() and
not f.getAbsolutePath() = "null" and
not f.getAbsolutePath().matches("%ActivXTabControl%.cs")
}
from Variable variable, string name
where name = variable.getName()
and variable.fromSource()
and sourceFile(variable.getFile())
and not allowedName(name)
and not allowedVariable(variable)
//
// Adjustable parameter:
//
and name.length() < 3
//
select variable, "Variable name '" + name + "' is too short."
//
// Adjustable: acceptable short names
//
predicate allowedName(string name)
{
name = "url" or name = "cmd" or name = "UK" or name = "uri" or
name = "top" or name = "row" or name = "pin" or name = "log" or
name = "key" or name = "_"
}
//
// Adjustable: variables that are allowed to have short names
//
predicate allowedVariable(Variable variable)
{
exists(Parameter param | variable = param and
not exists(param.getAnAccess()) and
param.getType().getName().matches("%EventArgs"))
or
exists(LocalVariable local | variable = local and
local.getVariableDeclExpr().getParent() instanceof CatchClause)
or
exists(Call c, LambdaExpr le |
le.getAParameter() = variable |
c.getAnArgument() = le
)
}