mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-03 01:16:14 +03:00
fix cyclomatic complexity lint triggering because of short circuit operations
This commit is contained in:
@@ -51,19 +51,21 @@ fn check<'a, 'tcx>(&mut self, cx: &'a LateContext<'a, 'tcx>, block: &Block, span
|
||||
let mut helper = CCHelper {
|
||||
match_arms: 0,
|
||||
divergence: 0,
|
||||
short_circuits: 0,
|
||||
tcx: &cx.tcx,
|
||||
};
|
||||
helper.visit_block(block);
|
||||
let CCHelper {
|
||||
match_arms,
|
||||
divergence,
|
||||
short_circuits,
|
||||
..
|
||||
} = helper;
|
||||
|
||||
if cc + divergence < match_arms {
|
||||
report_cc_bug(cx, cc, match_arms, divergence, span);
|
||||
if cc + divergence < match_arms + short_circuits {
|
||||
report_cc_bug(cx, cc, match_arms, divergence, short_circuits, span);
|
||||
} else {
|
||||
let rust_cc = cc + divergence - match_arms;
|
||||
let rust_cc = cc + divergence - match_arms - short_circuits;
|
||||
if rust_cc > self.limit.limit() {
|
||||
span_help_and_lint(cx,
|
||||
CYCLOMATIC_COMPLEXITY,
|
||||
@@ -105,6 +107,7 @@ fn exit_lint_attrs(&mut self, cx: &LateContext, attrs: &[Attribute]) {
|
||||
struct CCHelper<'a, 'tcx: 'a> {
|
||||
match_arms: u64,
|
||||
divergence: u64,
|
||||
short_circuits: u64, // && and ||
|
||||
tcx: &'a ty::TyCtxt<'tcx>,
|
||||
}
|
||||
|
||||
@@ -128,29 +131,38 @@ fn visit_expr(&mut self, e: &'a Expr) {
|
||||
}
|
||||
}
|
||||
ExprClosure(..) => {}
|
||||
ExprBinary(op, _, _) => {
|
||||
walk_expr(self, e);
|
||||
match op.node {
|
||||
BiAnd | BiOr => self.short_circuits += 1,
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
_ => walk_expr(self, e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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,
|
||||
&format!("Clippy encountered a bug calculating cyclomatic complexity: cc = {}, arms = {}, \
|
||||
div = {}. Please file a bug report.",
|
||||
div = {}, shorts = {}. Please file a bug report.",
|
||||
cc,
|
||||
narms,
|
||||
div));;
|
||||
div,
|
||||
shorts));;
|
||||
}
|
||||
#[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 {
|
||||
cx.sess().span_note_without_error(span,
|
||||
&format!("Clippy encountered a bug calculating cyclomatic complexity \
|
||||
(hide this message with `#[allow(cyclomatic_complexity)]`): cc \
|
||||
= {}, arms = {}, div = {}. Please file a bug report.",
|
||||
= {}, arms = {}, div = {}, shorts = {}. Please file a bug report.",
|
||||
cc,
|
||||
narms,
|
||||
div));
|
||||
div,
|
||||
shorts));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#![allow(cyclomatic_complexity)]
|
||||
use rustc::lint::*;
|
||||
use rustc_front::hir::*;
|
||||
use utils::{span_lint};
|
||||
@@ -38,12 +37,12 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
||||
], {
|
||||
if let BinOp_::BiLt = op.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_::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 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
||||
], {
|
||||
if let BinOp_::BiGt = op.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_::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.");
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
@@ -55,8 +55,6 @@ pub fn eq_block(&self, left: &Block, right: &Block) -> bool {
|
||||
both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
|
||||
}
|
||||
|
||||
// ok, it’s a big function, but mostly one big match with simples cases
|
||||
#[allow(cyclomatic_complexity)]
|
||||
pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
|
||||
if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
|
||||
return false;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#![feature(plugin, custom_attribute)]
|
||||
#![plugin(clippy)]
|
||||
#![deny(clippy)]
|
||||
#![allow(clippy)]
|
||||
#![deny(cyclomatic_complexity)]
|
||||
#![allow(unused)]
|
||||
|
||||
@@ -90,7 +90,7 @@ fn main() { //~ERROR the function has a cyclomatic complexity of 28
|
||||
}
|
||||
|
||||
#[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;
|
||||
'a: for i in 0..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"]
|
||||
fn baa() { //~ ERROR: the function has a cyclomatic complexity of 2
|
||||
let x = || match 99 {
|
||||
|
||||
@@ -7,28 +7,28 @@ fn main() {
|
||||
let a: u32 = 1;
|
||||
let b: u32 = 2;
|
||||
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 {
|
||||
@@ -58,4 +58,3 @@ fn main() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user