fix cyclomatic complexity lint triggering because of short circuit operations

This commit is contained in:
Oliver Schneider 2016-03-08 15:10:02 +01:00
parent 08b7931b6b
commit bf20b40664
5 changed files with 45 additions and 27 deletions

View file

@ -51,19 +51,21 @@ impl CyclomaticComplexity {
let mut helper = CCHelper { let mut helper = CCHelper {
match_arms: 0, match_arms: 0,
divergence: 0, divergence: 0,
short_circuits: 0,
tcx: &cx.tcx, tcx: &cx.tcx,
}; };
helper.visit_block(block); helper.visit_block(block);
let CCHelper { let CCHelper {
match_arms, match_arms,
divergence, divergence,
short_circuits,
.. ..
} = helper; } = helper;
if cc + divergence < match_arms { if cc + divergence < match_arms + short_circuits {
report_cc_bug(cx, cc, match_arms, divergence, span); report_cc_bug(cx, cc, match_arms, divergence, short_circuits, span);
} else { } else {
let rust_cc = cc + divergence - match_arms; let rust_cc = cc + divergence - match_arms - short_circuits;
if rust_cc > self.limit.limit() { if rust_cc > self.limit.limit() {
span_help_and_lint(cx, span_help_and_lint(cx,
CYCLOMATIC_COMPLEXITY, CYCLOMATIC_COMPLEXITY,
@ -105,6 +107,7 @@ impl LateLintPass for CyclomaticComplexity {
struct CCHelper<'a, 'tcx: 'a> { struct CCHelper<'a, 'tcx: 'a> {
match_arms: u64, match_arms: u64,
divergence: u64, divergence: u64,
short_circuits: u64, // && and ||
tcx: &'a ty::TyCtxt<'tcx>, tcx: &'a ty::TyCtxt<'tcx>,
} }
@ -128,29 +131,38 @@ impl<'a, 'b, 'tcx> Visitor<'a> for CCHelper<'b, 'tcx> {
} }
} }
ExprClosure(..) => {} ExprClosure(..) => {}
ExprBinary(op, _, _) => {
walk_expr(self, e);
match op.node {
BiAnd | BiOr => self.short_circuits += 1,
_ => {},
}
}
_ => walk_expr(self, e), _ => walk_expr(self, e),
} }
} }
} }
#[cfg(feature="debugging")] #[cfg(feature="debugging")]
fn report_cc_bug(cx: &LateContext, cc: u64, narms: u64, div: u64, span: Span) { fn report_cc_bug(cx: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, span: Span) {
cx.sess().span_bug(span, cx.sess().span_bug(span,
&format!("Clippy encountered a bug calculating cyclomatic complexity: cc = {}, arms = {}, \ &format!("Clippy encountered a bug calculating cyclomatic complexity: cc = {}, arms = {}, \
div = {}. Please file a bug report.", div = {}, shorts = {}. Please file a bug report.",
cc, cc,
narms, narms,
div));; div,
shorts));;
} }
#[cfg(not(feature="debugging"))] #[cfg(not(feature="debugging"))]
fn report_cc_bug(cx: &LateContext, cc: u64, narms: u64, div: u64, span: Span) { fn report_cc_bug(cx: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, span: Span) {
if cx.current_level(CYCLOMATIC_COMPLEXITY) != Level::Allow { if cx.current_level(CYCLOMATIC_COMPLEXITY) != Level::Allow {
cx.sess().span_note_without_error(span, cx.sess().span_note_without_error(span,
&format!("Clippy encountered a bug calculating cyclomatic complexity \ &format!("Clippy encountered a bug calculating cyclomatic complexity \
(hide this message with `#[allow(cyclomatic_complexity)]`): cc \ (hide this message with `#[allow(cyclomatic_complexity)]`): cc \
= {}, arms = {}, div = {}. Please file a bug report.", = {}, arms = {}, div = {}, shorts = {}. Please file a bug report.",
cc, cc,
narms, narms,
div)); div,
shorts));
} }
} }

View file

@ -1,4 +1,3 @@
#![allow(cyclomatic_complexity)]
use rustc::lint::*; use rustc::lint::*;
use rustc_front::hir::*; use rustc_front::hir::*;
use utils::{span_lint}; use utils::{span_lint};
@ -38,12 +37,12 @@ impl LateLintPass for OverflowCheckConditional {
], { ], {
if let BinOp_::BiLt = op.node { if let BinOp_::BiLt = op.node {
if let BinOp_::BiAdd = op2.node { if let BinOp_::BiAdd = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditions that will fail in Rust.");
} }
} }
if let BinOp_::BiGt = op.node { if let BinOp_::BiGt = op.node {
if let BinOp_::BiSub = op2.node { if let BinOp_::BiSub = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditions that will fail in Rust.");
} }
} }
}} }}
@ -60,12 +59,12 @@ impl LateLintPass for OverflowCheckConditional {
], { ], {
if let BinOp_::BiGt = op.node { if let BinOp_::BiGt = op.node {
if let BinOp_::BiAdd = op2.node { if let BinOp_::BiAdd = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditions that will fail in Rust.");
} }
} }
if let BinOp_::BiLt = op.node { if let BinOp_::BiLt = op.node {
if let BinOp_::BiSub = op2.node { if let BinOp_::BiSub = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditions that will fail in Rust.");
} }
} }
}} }}

View file

@ -55,8 +55,6 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r)) both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
} }
// ok, its a big function, but mostly one big match with simples cases
#[allow(cyclomatic_complexity)]
pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool { pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
if self.ignore_fn && differing_macro_contexts(left.span, right.span) { if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
return false; return false;

View file

@ -1,6 +1,6 @@
#![feature(plugin, custom_attribute)] #![feature(plugin, custom_attribute)]
#![plugin(clippy)] #![plugin(clippy)]
#![deny(clippy)] #![allow(clippy)]
#![deny(cyclomatic_complexity)] #![deny(cyclomatic_complexity)]
#![allow(unused)] #![allow(unused)]
@ -90,7 +90,7 @@ fn main() { //~ERROR the function has a cyclomatic complexity of 28
} }
#[cyclomatic_complexity = "0"] #[cyclomatic_complexity = "0"]
fn kaboom() { //~ ERROR: the function has a cyclomatic complexity of 8 fn kaboom() { //~ ERROR: the function has a cyclomatic complexity of 7
let n = 0; let n = 0;
'a: for i in 0..20 { 'a: for i in 0..20 {
'b: for j in i..20 { 'b: for j in i..20 {
@ -135,6 +135,16 @@ fn bloo() {
} }
} }
#[cyclomatic_complexity = "0"]
fn lots_of_short_circuits() -> bool { //~ ERROR: the function has a cyclomatic complexity of 1
true && false && true && false && true && false && true
}
#[cyclomatic_complexity = "0"]
fn lots_of_short_circuits2() -> bool { //~ ERROR: the function has a cyclomatic complexity of 1
true || false || true || false || true || false || true
}
#[cyclomatic_complexity = "0"] #[cyclomatic_complexity = "0"]
fn baa() { //~ ERROR: the function has a cyclomatic complexity of 2 fn baa() { //~ ERROR: the function has a cyclomatic complexity of 2
let x = || match 99 { let x = || match 99 {

View file

@ -7,28 +7,28 @@ fn main() {
let a: u32 = 1; let a: u32 = 1;
let b: u32 = 2; let b: u32 = 2;
let c: u32 = 3; let c: u32 = 3;
if a + b < a { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. if a + b < a { //~ERROR You are trying to use classic C overflow conditions that will fail in Rust.
} }
if a > a + b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. if a > a + b { //~ERROR You are trying to use classic C overflow conditions that will fail in Rust.
} }
if a + b < b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. if a + b < b { //~ERROR You are trying to use classic C overflow conditions that will fail in Rust.
} }
if b > a + b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. if b > a + b { //~ERROR You are trying to use classic C overflow conditions that will fail in Rust.
} }
if a - b > b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. if a - b > b { //~ERROR You are trying to use classic C underflow conditions that will fail in Rust.
} }
if b < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. if b < a - b { //~ERROR You are trying to use classic C underflow conditions that will fail in Rust.
} }
if a - b > a { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. if a - b > a { //~ERROR You are trying to use classic C underflow conditions that will fail in Rust.
} }
if a < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. if a < a - b { //~ERROR You are trying to use classic C underflow conditions that will fail in Rust.
} }
if a + b < c { if a + b < c {
@ -58,4 +58,3 @@ fn main() {
} }
} }