Skip to content

Commit eec4421

Browse files
committed
Update clippy
1 parent 3557e20 commit eec4421

25 files changed

+210
-372
lines changed

clippy_lints/src/cargo_common_metadata.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, fie
4444
}
4545

4646
fn is_empty_str(value: &Option<String>) -> bool {
47-
match value {
48-
None => true,
49-
Some(value) if value.is_empty() => true,
50-
_ => false,
51-
}
47+
value.as_ref().map_or(true, String::is_empty)
5248
}
5349

5450
fn is_empty_vec(value: &[String]) -> bool {
Lines changed: 25 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
//! calculate cognitive complexity and warn about overly complex functions
22
3-
use rustc::cfg::CFG;
43
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
54
use rustc::hir::*;
65
use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
7-
use rustc::ty;
86
use rustc::{declare_tool_lint, impl_lint_pass};
97
use syntax::ast::Attribute;
108
use syntax::source_map::Span;
119

12-
use crate::utils::{is_allowed, match_type, paths, span_help_and_lint, LimitStack};
10+
use crate::utils::{match_type, paths, span_help_and_lint, LimitStack};
1311

1412
declare_clippy_lint! {
1513
/// **What it does:** Checks for methods with high cognitive complexity.
@@ -46,30 +44,11 @@ impl CognitiveComplexity {
4644
return;
4745
}
4846

49-
let cfg = CFG::new(cx.tcx, body);
5047
let expr = &body.value;
51-
let n = cfg.graph.len_nodes() as u64;
52-
let e = cfg.graph.len_edges() as u64;
53-
if e + 2 < n {
54-
// the function has unreachable code, other lints should catch this
55-
return;
56-
}
57-
let cc = e + 2 - n;
58-
let mut helper = CCHelper {
59-
match_arms: 0,
60-
divergence: 0,
61-
short_circuits: 0,
62-
returns: 0,
63-
cx,
64-
};
48+
49+
let mut helper = CCHelper { cc: 1, returns: 0 };
6550
helper.visit_expr(expr);
66-
let CCHelper {
67-
match_arms,
68-
divergence,
69-
short_circuits,
70-
returns,
71-
..
72-
} = helper;
51+
let CCHelper { cc, returns } = helper;
7352
let ret_ty = cx.tables.node_type(expr.hir_id);
7453
let ret_adjust = if match_type(cx, ret_ty, &paths::RESULT) {
7554
returns
@@ -78,36 +57,23 @@ impl CognitiveComplexity {
7857
(returns / 2)
7958
};
8059

81-
if cc + divergence < match_arms + short_circuits {
82-
report_cc_bug(
60+
let mut rust_cc = cc;
61+
// prevent degenerate cases where unreachable code contains `return` statements
62+
if rust_cc >= ret_adjust {
63+
rust_cc -= ret_adjust;
64+
}
65+
if rust_cc > self.limit.limit() {
66+
span_help_and_lint(
8367
cx,
84-
cc,
85-
match_arms,
86-
divergence,
87-
short_circuits,
88-
ret_adjust,
68+
COGNITIVE_COMPLEXITY,
8969
span,
90-
body.id().hir_id,
70+
&format!(
71+
"the function has a cognitive complexity of ({}/{})",
72+
rust_cc,
73+
self.limit.limit()
74+
),
75+
"you could split it up into multiple smaller functions",
9176
);
92-
} else {
93-
let mut rust_cc = cc + divergence - match_arms - short_circuits;
94-
// prevent degenerate cases where unreachable code contains `return` statements
95-
if rust_cc >= ret_adjust {
96-
rust_cc -= ret_adjust;
97-
}
98-
if rust_cc > self.limit.limit() {
99-
span_help_and_lint(
100-
cx,
101-
COGNITIVE_COMPLEXITY,
102-
span,
103-
&format!(
104-
"the function has a cognitive complexity of ({}/{})",
105-
rust_cc,
106-
self.limit.limit()
107-
),
108-
"you could split it up into multiple smaller functions",
109-
);
110-
}
11177
}
11278
}
11379
}
@@ -136,99 +102,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CognitiveComplexity {
136102
}
137103
}
138104

139-
struct CCHelper<'a, 'tcx> {
140-
match_arms: u64,
141-
divergence: u64,
105+
struct CCHelper {
106+
cc: u64,
142107
returns: u64,
143-
short_circuits: u64, // && and ||
144-
cx: &'a LateContext<'a, 'tcx>,
145108
}
146109

147-
impl<'a, 'tcx> Visitor<'tcx> for CCHelper<'a, 'tcx> {
110+
impl<'tcx> Visitor<'tcx> for CCHelper {
148111
fn visit_expr(&mut self, e: &'tcx Expr) {
112+
walk_expr(self, e);
149113
match e.node {
150114
ExprKind::Match(_, ref arms, _) => {
151-
walk_expr(self, e);
152115
let arms_n: u64 = arms.iter().map(|arm| arm.pats.len() as u64).sum();
153116
if arms_n > 1 {
154-
self.match_arms += arms_n - 2;
155-
}
156-
},
157-
ExprKind::Call(ref callee, _) => {
158-
walk_expr(self, e);
159-
let ty = self.cx.tables.node_type(callee.hir_id);
160-
match ty.sty {
161-
ty::FnDef(..) | ty::FnPtr(_) => {
162-
let sig = ty.fn_sig(self.cx.tcx);
163-
if sig.skip_binder().output().sty == ty::Never {
164-
self.divergence += 1;
165-
}
166-
},
167-
_ => (),
168-
}
169-
},
170-
ExprKind::Closure(.., _) => (),
171-
ExprKind::Binary(op, _, _) => {
172-
walk_expr(self, e);
173-
match op.node {
174-
BinOpKind::And | BinOpKind::Or => self.short_circuits += 1,
175-
_ => (),
117+
self.cc += 1;
176118
}
119+
self.cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;
177120
},
178121
ExprKind::Ret(_) => self.returns += 1,
179-
_ => walk_expr(self, e),
122+
_ => {},
180123
}
181124
}
182125
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
183126
NestedVisitorMap::None
184127
}
185128
}
186-
187-
#[cfg(feature = "debugging")]
188-
#[allow(clippy::too_many_arguments)]
189-
fn report_cc_bug(
190-
_: &LateContext<'_, '_>,
191-
cc: u64,
192-
narms: u64,
193-
div: u64,
194-
shorts: u64,
195-
returns: u64,
196-
span: Span,
197-
_: HirId,
198-
) {
199-
span_bug!(
200-
span,
201-
"Clippy encountered a bug calculating cognitive complexity: cc = {}, arms = {}, \
202-
div = {}, shorts = {}, returns = {}. Please file a bug report.",
203-
cc,
204-
narms,
205-
div,
206-
shorts,
207-
returns
208-
);
209-
}
210-
#[cfg(not(feature = "debugging"))]
211-
#[allow(clippy::too_many_arguments)]
212-
fn report_cc_bug(
213-
cx: &LateContext<'_, '_>,
214-
cc: u64,
215-
narms: u64,
216-
div: u64,
217-
shorts: u64,
218-
returns: u64,
219-
span: Span,
220-
id: HirId,
221-
) {
222-
if !is_allowed(cx, COGNITIVE_COMPLEXITY, id) {
223-
cx.sess().span_note_without_error(
224-
span,
225-
&format!(
226-
"Clippy encountered a bug calculating cognitive complexity \
227-
(hide this message with `#[allow(cognitive_complexity)]`): \
228-
cc = {}, arms = {}, div = {}, shorts = {}, returns = {}. \
229-
Please file a bug report.",
230-
cc, narms, div, shorts, returns
231-
),
232-
);
233-
}
234-
}

clippy_lints/src/dbg_macro.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ impl EarlyLintPass for DbgMacro {
5959
fn tts_span(tts: TokenStream) -> Option<Span> {
6060
let mut cursor = tts.into_trees();
6161
let first = cursor.next()?.span();
62-
let span = match cursor.last() {
63-
Some(tree) => first.to(tree.span()),
64-
None => first,
65-
};
62+
let span = cursor.last().map_or(first, |tree| first.to(tree.span()));
6663
Some(span)
6764
}

clippy_lints/src/excessive_precision.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,15 @@ impl ExcessivePrecision {
9898
/// Ex `1_000_000_000.0`
9999
/// Ex `1_000_000_000.`
100100
fn dot_zero_exclusion(s: &str) -> bool {
101-
if let Some(after_dec) = s.split('.').nth(1) {
101+
s.split('.').nth(1).map_or(false, |after_dec| {
102102
let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
103103

104104
match decpart.next() {
105105
Some('0') => decpart.count() == 0,
106106
Some(_) => false,
107107
None => true,
108108
}
109-
} else {
110-
false
111-
}
109+
})
112110
}
113111

114112
fn max_digits(fty: FloatTy) -> u32 {

clippy_lints/src/functions.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,8 @@ impl<'a, 'tcx> Functions {
196196
let mut code_in_line;
197197

198198
// Skip the surrounding function decl.
199-
let start_brace_idx = match code_snippet.find('{') {
200-
Some(i) => i + 1,
201-
None => 0,
202-
};
203-
let end_brace_idx = match code_snippet.rfind('}') {
204-
Some(i) => i,
205-
None => code_snippet.len(),
206-
};
199+
let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
200+
let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
207201
let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
208202

209203
for mut line in function_lines {
@@ -223,14 +217,8 @@ impl<'a, 'tcx> Functions {
223217
None => break,
224218
}
225219
} else {
226-
let multi_idx = match line.find("/*") {
227-
Some(i) => i,
228-
None => line.len(),
229-
};
230-
let single_idx = match line.find("//") {
231-
Some(i) => i,
232-
None => line.len(),
233-
};
220+
let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
221+
let single_idx = line.find("//").unwrap_or_else(|| line.len());
234222
code_in_line |= multi_idx > 0 && single_idx > 0;
235223
// Implies multi_idx is below line.len()
236224
if multi_idx < single_idx {

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
815815
misc::CMP_OWNED,
816816
misc::FLOAT_CMP,
817817
misc::MODULO_ONE,
818-
misc::REDUNDANT_PATTERN,
819818
misc::SHORT_CIRCUIT_STATEMENT,
820819
misc::TOPLEVEL_REF_ARG,
821820
misc::ZERO_PTR,
@@ -824,6 +823,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
824823
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
825824
misc_early::MIXED_CASE_HEX_LITERALS,
826825
misc_early::REDUNDANT_CLOSURE_CALL,
826+
misc_early::REDUNDANT_PATTERN,
827827
misc_early::UNNEEDED_FIELD_PATTERN,
828828
misc_early::ZERO_PREFIXED_LITERAL,
829829
mut_reference::UNNECESSARY_MUT_PASSED,
@@ -967,13 +967,13 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
967967
methods::STRING_EXTEND_CHARS,
968968
methods::UNNECESSARY_FOLD,
969969
methods::WRONG_SELF_CONVENTION,
970-
misc::REDUNDANT_PATTERN,
971970
misc::TOPLEVEL_REF_ARG,
972971
misc::ZERO_PTR,
973972
misc_early::BUILTIN_TYPE_SHADOW,
974973
misc_early::DOUBLE_NEG,
975974
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
976975
misc_early::MIXED_CASE_HEX_LITERALS,
976+
misc_early::REDUNDANT_PATTERN,
977977
misc_early::UNNEEDED_FIELD_PATTERN,
978978
mut_reference::UNNECESSARY_MUT_PASSED,
979979
neg_multiply::NEG_MULTIPLY,

clippy_lints/src/loops.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc::{declare_lint_pass, declare_tool_lint};
1111
// use rustc::middle::region::CodeExtent;
1212
use crate::consts::{constant, Constant};
1313
use crate::utils::usage::mutated_variables;
14-
use crate::utils::{sext, sugg};
14+
use crate::utils::{sext, sugg, is_type_diagnostic_item};
1515
use rustc::middle::expr_use_visitor::*;
1616
use rustc::middle::mem_categorization::cmt_;
1717
use rustc::middle::mem_categorization::Categorization;
@@ -23,7 +23,7 @@ use std::iter::{once, Iterator};
2323
use std::mem;
2424
use syntax::ast;
2525
use syntax::source_map::Span;
26-
use syntax_pos::BytePos;
26+
use syntax_pos::{BytePos, Symbol};
2727

2828
use crate::utils::paths;
2929
use crate::utils::{
@@ -795,7 +795,7 @@ fn is_slice_like<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'_>) -> bool {
795795
_ => false,
796796
};
797797

798-
is_slice || match_type(cx, ty, &paths::VEC) || match_type(cx, ty, &paths::VEC_DEQUE)
798+
is_slice || is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) || match_type(cx, ty, &paths::VEC_DEQUE)
799799
}
800800

801801
fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr, var: HirId) -> Option<FixedOffsetVar> {
@@ -1950,7 +1950,7 @@ fn is_ref_iterable_type(cx: &LateContext<'_, '_>, e: &Expr) -> bool {
19501950
// will allow further borrows afterwards
19511951
let ty = cx.tables.expr_ty(e);
19521952
is_iterable_array(ty, cx) ||
1953-
match_type(cx, ty, &paths::VEC) ||
1953+
is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) ||
19541954
match_type(cx, ty, &paths::LINKED_LIST) ||
19551955
match_type(cx, ty, &paths::HASHMAP) ||
19561956
match_type(cx, ty, &paths::HASHSET) ||
@@ -2003,10 +2003,7 @@ fn extract_first_expr(block: &Block) -> Option<&Expr> {
20032003
fn is_simple_break_expr(expr: &Expr) -> bool {
20042004
match expr.node {
20052005
ExprKind::Break(dest, ref passed_expr) if dest.label.is_none() && passed_expr.is_none() => true,
2006-
ExprKind::Block(ref b, _) => match extract_first_expr(b) {
2007-
Some(subexpr) => is_simple_break_expr(subexpr),
2008-
None => false,
2009-
},
2006+
ExprKind::Block(ref b, _) => extract_first_expr(b).map_or(false, |subexpr| is_simple_break_expr(subexpr)),
20102007
_ => false,
20112008
}
20122009
}

0 commit comments

Comments
 (0)