Skip to content

Commit 98b2982

Browse files
committed
Merge pull request #738 from oli-obk/improve_cc
simplify cyclomatic complexity auxiliarly value computation
2 parents c0c2f61 + e7fa117 commit 98b2982

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

src/cyclomatic_complexity.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,22 @@ impl CyclomaticComplexity {
4848
let n = cfg.graph.len_nodes() as u64;
4949
let e = cfg.graph.len_edges() as u64;
5050
let cc = e + 2 - n;
51-
let mut arm_counter = MatchArmCounter(0);
52-
arm_counter.visit_block(block);
53-
let narms = arm_counter.0;
54-
55-
let mut diverge_counter = DivergenceCounter(0, &cx.tcx);
56-
diverge_counter.visit_block(block);
57-
let divergence = diverge_counter.0;
58-
59-
if cc + divergence < narms {
60-
report_cc_bug(cx, cc, narms, divergence, span);
51+
let mut helper = CCHelper {
52+
match_arms: 0,
53+
divergence: 0,
54+
tcx: &cx.tcx,
55+
};
56+
helper.visit_block(block);
57+
let CCHelper {
58+
match_arms,
59+
divergence,
60+
..
61+
} = helper;
62+
63+
if cc + divergence < match_arms {
64+
report_cc_bug(cx, cc, match_arms, divergence, span);
6165
} else {
62-
let rust_cc = cc + divergence - narms;
66+
let rust_cc = cc + divergence - match_arms;
6367
if rust_cc > self.limit.limit() {
6468
span_help_and_lint(cx,
6569
CYCLOMATIC_COMPLEXITY,
@@ -98,35 +102,28 @@ impl LateLintPass for CyclomaticComplexity {
98102
}
99103
}
100104

101-
struct MatchArmCounter(u64);
105+
struct CCHelper<'a, 'tcx: 'a> {
106+
match_arms: u64,
107+
divergence: u64,
108+
tcx: &'a ty::TyCtxt<'tcx>,
109+
}
102110

103-
impl<'a> Visitor<'a> for MatchArmCounter {
111+
impl<'a, 'b, 'tcx> Visitor<'a> for CCHelper<'b, 'tcx> {
104112
fn visit_expr(&mut self, e: &'a Expr) {
105113
match e.node {
106114
ExprMatch(_, ref arms, _) => {
107115
walk_expr(self, e);
108116
let arms_n: u64 = arms.iter().map(|arm| arm.pats.len() as u64).sum();
109117
if arms_n > 1 {
110-
self.0 += arms_n - 2;
118+
self.match_arms += arms_n - 2;
111119
}
112120
}
113-
ExprClosure(..) => {}
114-
_ => walk_expr(self, e),
115-
}
116-
}
117-
}
118-
119-
struct DivergenceCounter<'a, 'tcx: 'a>(u64, &'a ty::TyCtxt<'tcx>);
120-
121-
impl<'a, 'b, 'tcx> Visitor<'a> for DivergenceCounter<'b, 'tcx> {
122-
fn visit_expr(&mut self, e: &'a Expr) {
123-
match e.node {
124121
ExprCall(ref callee, _) => {
125122
walk_expr(self, e);
126-
let ty = self.1.node_id_to_type(callee.id);
123+
let ty = self.tcx.node_id_to_type(callee.id);
127124
if let ty::TyBareFn(_, ty) = ty.sty {
128125
if ty.sig.skip_binder().output.diverges() {
129-
self.0 += 1;
126+
self.divergence += 1;
130127
}
131128
}
132129
}

0 commit comments

Comments
 (0)