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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! `Cognitive` implementation for C#.
#![allow(
clippy::enum_glob_use,
clippy::match_same_arms,
clippy::needless_pass_by_value,
clippy::wildcard_imports
)]
#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
use super::*;
impl Cognitive for CsharpCode {
fn compute<'a>(
node: &Node<'a>,
_code: &'a [u8],
stats: &mut Stats,
nesting_map: &mut HashMap<usize, (usize, usize, usize)>,
) {
use Csharp::*;
let (mut nesting, mut depth, mut lambda) = get_nesting_from_map(node, nesting_map);
match node.kind_id().into() {
IfStatement if !Self::is_else_if(node) => {
increase_nesting(stats, &mut nesting, depth, lambda);
}
ForStatement
| ForeachStatement
| WhileStatement
| DoStatement
| SwitchStatement
| SwitchExpression
| CatchClause
| ConditionalExpression => {
increase_nesting(stats, &mut nesting, depth, lambda);
}
// `else` is an anonymous keyword token. Each occurrence carries
// a flat +1 for the alternative branch (matches Java's `Else`
// handling).
Else => {
increment_by_one(stats);
}
// Per SonarSource Cognitive Complexity §B2, any `goto` (including
// `goto label`, `goto case x`, `goto default`) is an unstructured
// jump and adds +1. C#'s grammar does not allow labeled
// `break`/`continue` (those forms are syntactically rejected), so
// the only labeled-jump form to handle here is `goto_statement`.
GotoStatement => {
increment_by_one(stats);
}
BinaryExpression => {
// C#'s null-coalescing `??` short-circuits like `&&` /
// `||` and forms boolean sequences alongside them.
// Mirrors the C# cyclomatic operator set.
compute_booleans_with(node, stats, |id| {
matches!(id.into(), AMPAMP | PIPEPIPE | QMARKQMARK)
});
}
AssignmentExpression => {
// C#'s compound null-coalescing assignment `??=` is
// semantically `x = x ?? y` and carries one boolean-
// sequence decision, parallel to the cyclomatic fix
// from #231. The operator token sits inside the
// `assignment_expression` node rather than a
// `BinaryExpression`, so it needs its own arm (#236).
// C# grammar does not provide `&&=` or `||=`, so only
// `??=` matters here.
compute_booleans_with(node, stats, |id| matches!(id.into(), QMARKQMARKEQ));
}
LambdaExpression | AnonymousMethodExpression => {
lambda += 1;
}
// At a (possibly nested) function boundary, reset structural
// nesting to zero and bump the function-depth surcharge when
// this declaration is itself nested inside another — matching
// Rust and the 9-of-13 sibling families. C# local
// functions are the acute case: a local function declared
// inside an `if` previously inherited `nesting = 1`, inflating
// every control-flow statement in its body by one. The grammar
// emits both `local_function_statement` and the aliased
// `local_function_declaration`; both are boundaries (#696).
MethodDeclaration
| ConstructorDeclaration
| DestructorDeclaration
| OperatorDeclaration
| ConversionOperatorDeclaration
| AccessorDeclaration
| LocalFunctionStatement
| LocalFunctionDeclaration => {
nesting = 0;
increment_function_depth(
&mut depth,
node,
&[
MethodDeclaration,
ConstructorDeclaration,
DestructorDeclaration,
OperatorDeclaration,
ConversionOperatorDeclaration,
AccessorDeclaration,
LocalFunctionStatement,
LocalFunctionDeclaration,
],
);
}
_ => {}
}
nesting_map.insert(node.id(), (nesting, depth, lambda));
}
}