diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index d44784843e73..575841cf4f93 100644 --- a/clippy_lints/src/cognitive_complexity.rs +++ b/clippy_lints/src/cognitive_complexity.rs @@ -1,15 +1,17 @@ //! calculate cognitive complexity and warn about overly complex functions -use rustc::cfg::CFG; -use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; -use rustc::hir::*; -use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass}; -use rustc::ty; +#![allow(clippy::cast_possible_truncation)] +#![allow(clippy::cast_precision_loss)] +#![allow(clippy::cast_sign_loss)] + +use rustc::lint::{EarlyContext, EarlyLintPass}; +use rustc::lint::{LintArray, LintContext, LintPass}; use rustc::{declare_tool_lint, lint_array}; use syntax::ast::Attribute; -use syntax::source_map::Span; +use syntax::ast::*; +use syntax::visit::{walk_expr, Visitor}; -use crate::utils::{in_macro, is_allowed, match_type, paths, span_help_and_lint, LimitStack}; +use crate::utils::{span_help_and_lint, LimitStack}; declare_clippy_lint! { /// **What it does:** Checks for methods with high cognitive complexity. @@ -20,7 +22,9 @@ declare_clippy_lint! { /// **Known problems:** Sometimes it's hard to find a way to reduce the /// complexity. /// - /// **Example:** No. You'll see it when you get the warning. + /// **Example:** Sorry. Examples are too big and varied to put in here. For a + /// complete explanation of the analysis being made though, you can read this paper: + /// https://www.sonarsource.com/docs/CognitiveComplexity.pdf pub COGNITIVE_COMPLEXITY, complexity, "functions that should be split up into multiple functions" @@ -48,190 +52,258 @@ impl LintPass for CognitiveComplexity { } } -impl CognitiveComplexity { - fn check<'a, 'tcx: 'a>(&mut self, cx: &'a LateContext<'a, 'tcx>, body: &'tcx Body, span: Span) { - if in_macro(span) { - return; - } +impl EarlyLintPass for CognitiveComplexity { + fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { + if let ItemKind::Fn(_, _, _, fn_block) = &item.node { + if !item.attrs.iter().any(|a| a.check_name("test")) { + let mut helper = CoCHelper { + current_nesting: 0, + score: 0.0, + }; - let cfg = CFG::new(cx.tcx, body); - let expr = &body.value; - let n = cfg.graph.len_nodes() as u64; - let e = cfg.graph.len_edges() as u64; - if e + 2 < n { - // the function has unreachable code, other lints should catch this - return; - } - let cc = e + 2 - n; - let mut helper = CCHelper { - match_arms: 0, - divergence: 0, - short_circuits: 0, - returns: 0, - cx, - }; - helper.visit_expr(expr); - let CCHelper { - match_arms, - divergence, - short_circuits, - returns, - .. - } = helper; - let ret_ty = cx.tables.node_type(expr.hir_id); - let ret_adjust = if match_type(cx, ret_ty, &paths::RESULT) { - returns - } else { - returns / 2 - }; - - if cc + divergence < match_arms + short_circuits { - report_cc_bug( - cx, - cc, - match_arms, - divergence, - short_circuits, - ret_adjust, - span, - body.id().hir_id, - ); - } else { - let mut rust_cc = cc + divergence - match_arms - short_circuits; - // prevent degenerate cases where unreachable code contains `return` statements - if rust_cc >= ret_adjust { - rust_cc -= ret_adjust; - } - if rust_cc > self.limit.limit() { - span_help_and_lint( - cx, - COGNITIVE_COMPLEXITY, - span, - &format!("the function has a cognitive complexity of {}", rust_cc), - "you could split it up into multiple smaller functions", - ); - } - } - } -} + helper.visit_block(&fn_block); + + let fn_score = helper.score as u64; + let score_limit = self.limit.limit(); -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CognitiveComplexity { - fn check_fn( - &mut self, - cx: &LateContext<'a, 'tcx>, - _: intravisit::FnKind<'tcx>, - _: &'tcx FnDecl, - body: &'tcx Body, - span: Span, - hir_id: HirId, - ) { - let def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id); - if !cx.tcx.has_attr(def_id, "test") { - self.check(cx, body, span); + if fn_score > score_limit { + span_help_and_lint( + cx, + COGNITIVE_COMPLEXITY, + item.span, + &format!("the function has a cognitive complexity of {}", fn_score), + "you could split it up into multiple smaller functions", + ); + } + } } } - fn enter_lint_attrs(&mut self, cx: &LateContext<'a, 'tcx>, attrs: &'tcx [Attribute]) { + fn enter_lint_attrs(&mut self, cx: &EarlyContext<'_>, attrs: &[Attribute]) { self.limit.push_attrs(cx.sess(), attrs, "cognitive_complexity"); } - fn exit_lint_attrs(&mut self, cx: &LateContext<'a, 'tcx>, attrs: &'tcx [Attribute]) { + fn exit_lint_attrs(&mut self, cx: &EarlyContext<'_>, attrs: &[Attribute]) { self.limit.pop_attrs(cx.sess(), attrs, "cognitive_complexity"); } } -struct CCHelper<'a, 'tcx: 'a> { - match_arms: u64, - divergence: u64, - returns: u64, - short_circuits: u64, // && and || - cx: &'a LateContext<'a, 'tcx>, +struct CoCHelper { + current_nesting: u64, + score: f64, } -impl<'a, 'tcx> Visitor<'tcx> for CCHelper<'a, 'tcx> { - fn visit_expr(&mut self, e: &'tcx Expr) { - match e.node { - ExprKind::Match(_, ref arms, _) => { - walk_expr(self, e); - let arms_n: u64 = arms.iter().map(|arm| arm.pats.len() as u64).sum(); - if arms_n > 1 { - self.match_arms += arms_n - 2; - } +impl CoCHelper { + fn push_nesting(&mut self) { + self.current_nesting += 1; + } + + fn pop_nesting(&mut self) { + assert!(self.current_nesting > 0); + self.current_nesting -= 1; + } + + fn add_to_score(&mut self, amount: u64) { + self.score += amount as f64; + } + + fn score_nd_structure(&mut self) { + self.add_to_score(self.current_nesting); + self.score_ni_structure(); + } + + fn score_ni_structure(&mut self) { + self.add_to_score(1); + } +} + +// FIXME: delete these notes + +/* + Notes on debugging this (14/4): + // tests/ui/cognitive_complexity.rs + * Check why is 330 not being flagged (it has CoC 0 atm) +*/ + +impl<'ast> Visitor<'ast> for CoCHelper { + /* + # Implemented here: + + ## Nesting Structures + IMPORTANT: ExprKind::Block(..) + already covers all cases. + ## (Nesting-Dependent) Increments + if + match + for, while, loop + ## (Nesting-Independent) Increments + break, continue + Sequences of binary logical operators + Recursive function calls + Macro calls + */ + + fn visit_expr(&mut self, ex: &'ast Expr) { + match ex.node { + // Nesting-Increasing (the one and only) + ExprKind::Block(..) => { + self.push_nesting(); + walk_expr(self, ex); + self.pop_nesting(); + }, + + ExprKind::Closure(.., _) | ExprKind::IfLet(..) => { + // If Let is free of own increment. + // This is because it's a language construct + // that's specifically designed to save on complexity. + walk_expr(self, ex); + }, + + // FIXME (FAR FUTURE): make a separate, documented case, for recursive calls, + // such that it's treated differently from function or method calls. + ExprKind::Call(..) | ExprKind::MethodCall(..) => { + self.score_ni_structure(); + walk_expr(self, ex); + }, + + // Nesting-Dependent + ExprKind::If(..) + | ExprKind::Match(..) + | ExprKind::ForLoop(..) + | ExprKind::While(..) + | ExprKind::Loop(..) => { + // (For the IF-Case) + // Important: this pays for one "if" and one "else". + // Every "if" in an "else if" comes here again to pay + // for itself and its subsequent else. + self.score_nd_structure(); + walk_expr(self, ex); + }, + + // Nesting-Independent + ExprKind::Mac(ref mac) => { + self.visit_mac(mac); }, - ExprKind::Call(ref callee, _) => { - walk_expr(self, e); - let ty = self.cx.tables.node_type(callee.hir_id); - match ty.sty { - ty::FnDef(..) | ty::FnPtr(_) => { - let sig = ty.fn_sig(self.cx.tcx); - if sig.skip_binder().output().sty == ty::Never { - self.divergence += 1; + + // Nesting-Independent + ExprKind::Break(_, _) | ExprKind::Continue(_) => { + self.score_ni_structure(); + }, + + // When boolean operators change, we add 1 to the score. + ExprKind::Binary(binop, ref l_ex, ref r_ex) => { + // A logical operator + enum LogOp { + And, + Or, + } + + // The rightmost logical operator inside of an expression + fn rightmost_lop(expr: &Expr) -> Option { + match &expr.node { + ExprKind::Binary(binop, ref l_ex, _) => { + if let Some(log_op) = rightmost_lop(l_ex) { + Some(log_op) + } else { + match binop.node { + BinOpKind::And => Some(LogOp::And), + BinOpKind::Or => Some(LogOp::Or), + _ => None, + } + } + }, + ExprKind::Paren(expr) | ExprKind::Unary(_, expr) => rightmost_lop(&expr), + _ => None, + } + } + + // The leftmost logical operator inside of an expression + fn leftmost_lop(expr: &Expr) -> Option { + match &expr.node { + ExprKind::Binary(binop, _, ref r_ex) => { + if let Some(log_op) = leftmost_lop(r_ex) { + Some(log_op) + } else { + match binop.node { + BinOpKind::And => Some(LogOp::And), + BinOpKind::Or => Some(LogOp::Or), + _ => None, + } + } + }, + ExprKind::Paren(expr) | ExprKind::Unary(_, expr) => leftmost_lop(&expr), + _ => None, + } + } + + match binop.node { + BinOpKind::And => { + if let Some(LogOp::Or) = leftmost_lop(r_ex) { + self.score_ni_structure(); + } + if let Some(LogOp::Or) = rightmost_lop(l_ex) { + self.score_ni_structure(); } }, - _ => (), + BinOpKind::Or => { + if let Some(LogOp::And) = leftmost_lop(r_ex) { + self.score_ni_structure(); + } + if let Some(LogOp::And) = rightmost_lop(l_ex) { + self.score_ni_structure(); + } + }, + _ => {}, } + + walk_expr(self, ex); }, - ExprKind::Closure(.., _) => (), - ExprKind::Binary(op, _, _) => { - walk_expr(self, e); - match op.node { - BinOpKind::And | BinOpKind::Or => self.short_circuits += 1, - _ => (), - } + + // ### Probably non-scoring cases ### + // FIXME: make this explicit in the Draft. + + // THOUGHT: these shouldn't increase the score. + // ExprKind::Path(..) => {}, + // ExprKind::Range(..) => {}, + // ExprKind::Index(..) => {}, + // ExprKind::Field(..) => {}, + // ExprKind::Tup(..) => {}, + // ExprKind::Box(..) => {}, + // ExprKind::ObsoleteInPlace(..) => {}, + // ExprKind::Array(..) => {}, + // ExprKind::AssignOp(..) => {}, + // ExprKind::Assign(..) => {}, + // ExprKind::Lit(..) => {}, + // ExprKind::Struct(..) => {}, + // ExprKind::Repeat(..) => {}, + // ExprKind::AddrOf(..) => {}, + + // FIXME: examine these more closely + // ExprKind::Paren(..) => {}, + // ExprKind::Unary(..) => {}, + // ExprKind::Ret(..) => {}, + + // ### Undiscussed Cases ### + // FIXME: discuss and solve each one of these. + // ExprKind::Cast(..) => {}, + // ExprKind::Type(..) => {}, + // ExprKind::WhileLet(..) => {}, + // ExprKind::Async(..) => {}, + // ExprKind::Try(..) => {}, + // ExprKind::TryBlock(..) => {}, + // ExprKind::Yield(..) => {}, + // ExprKind::InlineAsm(..) => {}, + // ExprKind::Err => {}, + _ => { + // walk_expr(self, ex); }, - ExprKind::Ret(_) => self.returns += 1, - _ => walk_expr(self, e), } } - fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { - NestedVisitorMap::None - } -} -#[cfg(feature = "debugging")] -#[allow(clippy::too_many_arguments)] -fn report_cc_bug( - _: &LateContext<'_, '_>, - cc: u64, - narms: u64, - div: u64, - shorts: u64, - returns: u64, - span: Span, - _: HirId, -) { - span_bug!( - span, - "Clippy encountered a bug calculating cognitive complexity: cc = {}, arms = {}, \ - div = {}, shorts = {}, returns = {}. Please file a bug report.", - cc, - narms, - div, - shorts, - returns - ); -} -#[cfg(not(feature = "debugging"))] -#[allow(clippy::too_many_arguments)] -fn report_cc_bug( - cx: &LateContext<'_, '_>, - cc: u64, - narms: u64, - div: u64, - shorts: u64, - returns: u64, - span: Span, - id: HirId, -) { - if !is_allowed(cx, COGNITIVE_COMPLEXITY, id) { - cx.sess().span_note_without_error( - span, - &format!( - "Clippy encountered a bug calculating cognitive complexity \ - (hide this message with `#[allow(cognitive_complexity)]`): \ - cc = {}, arms = {}, div = {}, shorts = {}, returns = {}. \ - Please file a bug report.", - cc, narms, div, shorts, returns - ), - ); + fn visit_mac(&mut self, _mac: &Mac) { + // We override this so that the compiler + // doesn't panic. See the original implementation + // of `visit_mac` at rustc's src/libsyntax/visit.rs + // to know what normally happens. + self.score_ni_structure(); } } diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index d547e0a99b79..97dcca4ea9a7 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -306,6 +306,13 @@ pub fn register_pre_expansion_lints( ); store.register_pre_expansion_pass(Some(session), true, false, box attrs::CfgAttrPass); store.register_pre_expansion_pass(Some(session), true, false, box dbg_macro::Pass); + + store.register_pre_expansion_pass( + Some(session), + true, + false, + box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold), + ) } #[doc(hidden)] @@ -370,6 +377,7 @@ pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf { /// /// Used in `./src/driver.rs`. #[allow(clippy::too_many_lines)] +#[allow(clippy::cognitive_complexity)] #[rustfmt::skip] pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { let mut store = reg.sess.lint_store.borrow_mut(); @@ -478,9 +486,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_late_lint_pass(box no_effect::Pass); reg.register_late_lint_pass(box temporary_assignment::Pass); reg.register_late_lint_pass(box transmute::Transmute); - reg.register_late_lint_pass( - box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold) - ); reg.register_late_lint_pass(box escape::Pass{too_large_for_stack: conf.too_large_for_stack}); reg.register_early_lint_pass(box misc_early::MiscEarly); reg.register_late_lint_pass(box panic_unimplemented::Pass); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 2c96d9a8b5aa..0fa22d992391 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -109,7 +109,7 @@ define_Conf! { /// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about (blacklisted_names, "blacklisted_names", ["foo", "bar", "baz", "quux"] => Vec), /// Lint: COGNITIVE_COMPLEXITY. The maximum cognitive complexity a function can have - (cognitive_complexity_threshold, "cognitive_complexity_threshold", 25 => u64), + (cognitive_complexity_threshold, "cognitive_complexity_threshold", 50 => u64), /// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. Use the Cognitive Complexity lint instead. (cyclomatic_complexity_threshold, "cyclomatic_complexity_threshold", None => Option), /// Lint: DOC_MARKDOWN. The list of words this lint should not consider as identifiers needing ticks diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index 219503922701..c7432a012653 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -153,6 +153,7 @@ fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool { get_attr(sess, attrs, "dump").count() > 0 } +#[allow(clippy::cognitive_complexity)] #[allow(clippy::similar_names)] fn print_expr(cx: &LateContext<'_, '_>, expr: &hir::Expr, indent: usize) { let ind = " ".repeat(indent); @@ -415,6 +416,7 @@ fn print_item(cx: &LateContext<'_, '_>, item: &hir::Item) { } } +#[allow(clippy::cognitive_complexity)] #[allow(clippy::similar_names)] fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) { let ind = " ".repeat(indent); diff --git a/tests/ui/cognitive_complexity.rs b/tests/ui/cognitive_complexity.rs index 4e4016e78c2a..a5bd43d2c8aa 100644 --- a/tests/ui/cognitive_complexity.rs +++ b/tests/ui/cognitive_complexity.rs @@ -133,16 +133,6 @@ fn bloo() { } } -#[clippy::cognitive_complexity = "0"] -fn lots_of_short_circuits() -> bool { - true && false && true && false && true && false && true -} - -#[clippy::cognitive_complexity = "0"] -fn lots_of_short_circuits2() -> bool { - true || false || true || false || true || false || true -} - #[clippy::cognitive_complexity = "0"] fn baa() { let x = || match 99 { @@ -369,3 +359,20 @@ fn early_ret() -> i32 { _ => return 6, } } + +#[clippy::cognitive_complexity = "0"] +fn osscilating_logical_chain_1() -> bool { + true && false || true && false +} + +// This tests that the only thing that matters +// is the change in operator +#[clippy::cognitive_complexity = "0"] +fn osscilating_logical_chain_2() -> bool { + true && false && true && false || true && false && true && false +} + +#[clippy::cognitive_complexity = "0"] +fn osscilating_logical_chain_3() -> bool { + (true && false) || (true && false) +} \ No newline at end of file diff --git a/tests/ui/cognitive_complexity.stderr b/tests/ui/cognitive_complexity.stderr index 168653b97116..23c617166760 100644 --- a/tests/ui/cognitive_complexity.stderr +++ b/tests/ui/cognitive_complexity.stderr @@ -1,4 +1,4 @@ -error: the function has a cognitive complexity of 28 +error: the function has a cognitive complexity of 54 --> $DIR/cognitive_complexity.rs:6:1 | LL | / fn main() { @@ -13,7 +13,7 @@ LL | | } = note: `-D clippy::cognitive-complexity` implied by `-D warnings` = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 7 +error: the function has a cognitive complexity of 11 --> $DIR/cognitive_complexity.rs:91:1 | LL | / fn kaboom() { @@ -27,29 +27,9 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 1 +error: the function has a cognitive complexity of 5 --> $DIR/cognitive_complexity.rs:137:1 | -LL | / fn lots_of_short_circuits() -> bool { -LL | | true && false && true && false && true && false && true -LL | | } - | |_^ - | - = help: you could split it up into multiple smaller functions - -error: the function has a cognitive complexity of 1 - --> $DIR/cognitive_complexity.rs:142:1 - | -LL | / fn lots_of_short_circuits2() -> bool { -LL | | true || false || true || false || true || false || true -LL | | } - | |_^ - | - = help: you could split it up into multiple smaller functions - -error: the function has a cognitive complexity of 2 - --> $DIR/cognitive_complexity.rs:147:1 - | LL | / fn baa() { LL | | let x = || match 99 { LL | | 0 => 0, @@ -61,23 +41,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 2 - --> $DIR/cognitive_complexity.rs:148:13 - | -LL | let x = || match 99 { - | _____________^ -LL | | 0 => 0, -LL | | 1 => 1, -LL | | 2 => 2, -... | -LL | | _ => 42, -LL | | }; - | |_____^ - | - = help: you could split it up into multiple smaller functions - -error: the function has a cognitive complexity of 2 - --> $DIR/cognitive_complexity.rs:165:1 +error: the function has a cognitive complexity of 3 + --> $DIR/cognitive_complexity.rs:155:1 | LL | / fn bar() { LL | | match 99 { @@ -89,8 +54,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 2 - --> $DIR/cognitive_complexity.rs:184:1 +error: the function has a cognitive complexity of 5 + --> $DIR/cognitive_complexity.rs:174:1 | LL | / fn barr() { LL | | match 99 { @@ -103,8 +68,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 3 - --> $DIR/cognitive_complexity.rs:194:1 +error: the function has a cognitive complexity of 10 + --> $DIR/cognitive_complexity.rs:184:1 | LL | / fn barr2() { LL | | match 99 { @@ -117,8 +82,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 2 - --> $DIR/cognitive_complexity.rs:210:1 +error: the function has a cognitive complexity of 5 + --> $DIR/cognitive_complexity.rs:200:1 | LL | / fn barrr() { LL | | match 99 { @@ -131,8 +96,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 3 - --> $DIR/cognitive_complexity.rs:220:1 +error: the function has a cognitive complexity of 10 + --> $DIR/cognitive_complexity.rs:210:1 | LL | / fn barrr2() { LL | | match 99 { @@ -145,8 +110,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 2 - --> $DIR/cognitive_complexity.rs:236:1 +error: the function has a cognitive complexity of 5 + --> $DIR/cognitive_complexity.rs:226:1 | LL | / fn barrrr() { LL | | match 99 { @@ -159,8 +124,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 3 - --> $DIR/cognitive_complexity.rs:246:1 +error: the function has a cognitive complexity of 10 + --> $DIR/cognitive_complexity.rs:236:1 | LL | / fn barrrr2() { LL | | match 99 { @@ -173,8 +138,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 2 - --> $DIR/cognitive_complexity.rs:262:1 +error: the function has a cognitive complexity of 4 + --> $DIR/cognitive_complexity.rs:252:1 | LL | / fn cake() { LL | | if 4 == 5 { @@ -187,8 +152,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 4 - --> $DIR/cognitive_complexity.rs:272:1 +error: the function has a cognitive complexity of 10 + --> $DIR/cognitive_complexity.rs:262:1 | LL | / pub fn read_file(input_path: &str) -> String { LL | | use std::fs::File; @@ -201,8 +166,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 1 - --> $DIR/cognitive_complexity.rs:303:1 +error: the function has a cognitive complexity of 2 + --> $DIR/cognitive_complexity.rs:293:1 | LL | / fn void(void: Void) { LL | | if true { @@ -213,8 +178,19 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 1 - --> $DIR/cognitive_complexity.rs:316:1 +error: the function has a cognitive complexity of 2 + --> $DIR/cognitive_complexity.rs:300:1 + | +LL | / fn mcarton_sees_all() { +LL | | panic!("meh"); +LL | | panic!("möh"); +LL | | } + | |_^ + | + = help: you could split it up into multiple smaller functions + +error: the function has a cognitive complexity of 2 + --> $DIR/cognitive_complexity.rs:306:1 | LL | / fn try() -> Result { LL | | match 5 { @@ -226,8 +202,8 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 1 - --> $DIR/cognitive_complexity.rs:324:1 +error: the function has a cognitive complexity of 10 + --> $DIR/cognitive_complexity.rs:314:1 | LL | / fn try_again() -> Result { LL | | let _ = try!(Ok(42)); @@ -240,29 +216,45 @@ LL | | } | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 1 - --> $DIR/cognitive_complexity.rs:340:1 +error: the function has a cognitive complexity of 13 + --> $DIR/cognitive_complexity.rs:344:1 | -LL | / fn early() -> Result { -LL | | return Ok(5); -LL | | return Ok(5); -LL | | return Ok(5); +LL | / fn early_ret() -> i32 { +LL | | let a = if true { 42 } else { return 0; }; +LL | | let a = if a < 99 { 42 } else { return 0; }; +LL | | let a = if a < 99 { 42 } else { return 0; }; ... | -LL | | return Ok(5); +LL | | } LL | | } | |_^ | = help: you could split it up into multiple smaller functions -error: the function has a cognitive complexity of 8 - --> $DIR/cognitive_complexity.rs:354:1 +error: the function has a cognitive complexity of 2 + --> $DIR/cognitive_complexity.rs:364:1 | -LL | / fn early_ret() -> i32 { -LL | | let a = if true { 42 } else { return 0; }; -LL | | let a = if a < 99 { 42 } else { return 0; }; -LL | | let a = if a < 99 { 42 } else { return 0; }; -... | -LL | | } +LL | / fn osscilating_logical_chain_1() -> bool { +LL | | true && false || true && false +LL | | } + | |_^ + | + = help: you could split it up into multiple smaller functions + +error: the function has a cognitive complexity of 2 + --> $DIR/cognitive_complexity.rs:371:1 + | +LL | / fn osscilating_logical_chain_2() -> bool { +LL | | true && false && true && false || true && false && true && false +LL | | } + | |_^ + | + = help: you could split it up into multiple smaller functions + +error: the function has a cognitive complexity of 2 + --> $DIR/cognitive_complexity.rs:376:1 + | +LL | / fn osscilating_logical_chain_3() -> bool { +LL | | (true && false) || (true && false) LL | | } | |_^ | diff --git a/tests/ui/cognitive_complexity_attr_used.stderr b/tests/ui/cognitive_complexity_attr_used.stderr index 2cf41506f566..1ec62105e8be 100644 --- a/tests/ui/cognitive_complexity_attr_used.stderr +++ b/tests/ui/cognitive_complexity_attr_used.stderr @@ -1,4 +1,4 @@ -error: the function has a cognitive complexity of 3 +error: the function has a cognitive complexity of 4 --> $DIR/cognitive_complexity_attr_used.rs:9:1 | LL | / fn kaboom() { diff --git a/tests/ui/eta.fixed b/tests/ui/eta.fixed index 7e487fe21679..7526db8a1dab 100644 --- a/tests/ui/eta.fixed +++ b/tests/ui/eta.fixed @@ -7,8 +7,9 @@ clippy::many_single_char_names, clippy::needless_pass_by_value, clippy::option_map_unit_fn, - clippy::trivially_copy_pass_by_ref -)] + clippy::trivially_copy_pass_by_ref, + clippy::cognitive_complexity + )] #![warn(clippy::redundant_closure, clippy::needless_borrow)] use std::path::PathBuf; diff --git a/tests/ui/eta.rs b/tests/ui/eta.rs index a0d83c438251..d37c62314a05 100644 --- a/tests/ui/eta.rs +++ b/tests/ui/eta.rs @@ -7,8 +7,9 @@ clippy::many_single_char_names, clippy::needless_pass_by_value, clippy::option_map_unit_fn, - clippy::trivially_copy_pass_by_ref -)] + clippy::trivially_copy_pass_by_ref, + clippy::cognitive_complexity + )] #![warn(clippy::redundant_closure, clippy::needless_borrow)] use std::path::PathBuf; diff --git a/tests/ui/eta.stderr b/tests/ui/eta.stderr index 77423694f815..072e012942e8 100644 --- a/tests/ui/eta.stderr +++ b/tests/ui/eta.stderr @@ -1,5 +1,5 @@ error: redundant closure found - --> $DIR/eta.rs:17:27 + --> $DIR/eta.rs:18:27 | LL | let a = Some(1u8).map(|a| foo(a)); | ^^^^^^^^^^ help: remove closure as shown: `foo` @@ -7,19 +7,19 @@ LL | let a = Some(1u8).map(|a| foo(a)); = note: `-D clippy::redundant-closure` implied by `-D warnings` error: redundant closure found - --> $DIR/eta.rs:18:10 + --> $DIR/eta.rs:19:10 | LL | meta(|a| foo(a)); | ^^^^^^^^^^ help: remove closure as shown: `foo` error: redundant closure found - --> $DIR/eta.rs:19:27 + --> $DIR/eta.rs:20:27 | LL | let c = Some(1u8).map(|a| {1+2; foo}(a)); | ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `{1+2; foo}` error: this expression borrows a reference that is immediately dereferenced by the compiler - --> $DIR/eta.rs:21:21 + --> $DIR/eta.rs:22:21 | LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted | ^^^ help: change this to: `&2` @@ -27,43 +27,43 @@ LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted = note: `-D clippy::needless-borrow` implied by `-D warnings` error: redundant closure found - --> $DIR/eta.rs:28:27 + --> $DIR/eta.rs:29:27 | LL | let e = Some(1u8).map(|a| generic(a)); | ^^^^^^^^^^^^^^ help: remove closure as shown: `generic` error: redundant closure found - --> $DIR/eta.rs:71:51 + --> $DIR/eta.rs:72:51 | LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo()); | ^^^^^^^^^^^ help: remove closure as shown: `TestStruct::foo` error: redundant closure found - --> $DIR/eta.rs:73:51 + --> $DIR/eta.rs:74:51 | LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo()); | ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `TestTrait::trait_foo` error: redundant closure found - --> $DIR/eta.rs:76:42 + --> $DIR/eta.rs:77:42 | LL | let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear()); | ^^^^^^^^^^^^^ help: remove closure as shown: `std::vec::Vec::clear` error: redundant closure found - --> $DIR/eta.rs:81:29 + --> $DIR/eta.rs:82:29 | LL | let e = Some("str").map(|s| s.to_string()); | ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `std::string::ToString::to_string` error: redundant closure found - --> $DIR/eta.rs:83:27 + --> $DIR/eta.rs:84:27 | LL | let e = Some('a').map(|s| s.to_uppercase()); | ^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_uppercase` error: redundant closure found - --> $DIR/eta.rs:86:65 + --> $DIR/eta.rs:87:65 | LL | let e: std::vec::Vec = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_ascii_uppercase` diff --git a/tests/ui/expect_fun_call.fixed b/tests/ui/expect_fun_call.fixed index e111ee3dfeda..00830fa13403 100644 --- a/tests/ui/expect_fun_call.fixed +++ b/tests/ui/expect_fun_call.fixed @@ -1,6 +1,7 @@ // run-rustfix #![warn(clippy::expect_fun_call)] +#![allow(clippy::cognitive_complexity)] /// Checks implementation of the `EXPECT_FUN_CALL` lint diff --git a/tests/ui/expect_fun_call.rs b/tests/ui/expect_fun_call.rs index 891ec883120c..546d084c7aa2 100644 --- a/tests/ui/expect_fun_call.rs +++ b/tests/ui/expect_fun_call.rs @@ -1,6 +1,7 @@ // run-rustfix #![warn(clippy::expect_fun_call)] +#![allow(clippy::cognitive_complexity)] /// Checks implementation of the `EXPECT_FUN_CALL` lint diff --git a/tests/ui/expect_fun_call.stderr b/tests/ui/expect_fun_call.stderr index bb16fabd973b..033698c78624 100644 --- a/tests/ui/expect_fun_call.stderr +++ b/tests/ui/expect_fun_call.stderr @@ -1,5 +1,5 @@ error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:28:26 + --> $DIR/expect_fun_call.rs:29:26 | LL | with_none_and_format.expect(&format!("Error {}: fake error", error_code)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))` @@ -7,61 +7,61 @@ LL | with_none_and_format.expect(&format!("Error {}: fake error", error_code = note: `-D clippy::expect-fun-call` implied by `-D warnings` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:31:26 + --> $DIR/expect_fun_call.rs:32:26 | LL | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:41:25 + --> $DIR/expect_fun_call.rs:42:25 | LL | with_err_and_format.expect(&format!("Error {}: fake error", error_code)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:44:25 + --> $DIR/expect_fun_call.rs:45:25 | LL | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:56:17 + --> $DIR/expect_fun_call.rs:57:17 | LL | Some("foo").expect(format!("{} {}", 1, 2).as_ref()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("{} {}", 1, 2))` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:77:21 + --> $DIR/expect_fun_call.rs:78:21 | LL | Some("foo").expect(&get_string()); | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!(get_string()) })` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:78:21 + --> $DIR/expect_fun_call.rs:79:21 | LL | Some("foo").expect(get_string().as_ref()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!(get_string()) })` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:79:21 + --> $DIR/expect_fun_call.rs:80:21 | LL | Some("foo").expect(get_string().as_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!(get_string()) })` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:81:21 + --> $DIR/expect_fun_call.rs:82:21 | LL | Some("foo").expect(get_static_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!(get_static_str()) })` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:82:21 + --> $DIR/expect_fun_call.rs:83:21 | LL | Some("foo").expect(get_non_static_str(&0)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!(get_non_static_str(&0).to_string()) })` error: use of `expect` followed by a function call - --> $DIR/expect_fun_call.rs:86:16 + --> $DIR/expect_fun_call.rs:87:16 | LL | Some(true).expect(&format!("key {}, {}", 1, 2)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("key {}, {}", 1, 2))` diff --git a/tests/ui/explicit_write.fixed b/tests/ui/explicit_write.fixed index 692d2ca675f9..10fb36e3bbaa 100644 --- a/tests/ui/explicit_write.fixed +++ b/tests/ui/explicit_write.fixed @@ -1,5 +1,8 @@ // run-rustfix -#![allow(unused_imports)] +#![allow( + unused_imports, + clippy::cognitive_complexity + )] #![warn(clippy::explicit_write)] fn stdout() -> String { diff --git a/tests/ui/explicit_write.rs b/tests/ui/explicit_write.rs index 455c5ef55d05..43ddfb1a4b40 100644 --- a/tests/ui/explicit_write.rs +++ b/tests/ui/explicit_write.rs @@ -1,5 +1,8 @@ // run-rustfix -#![allow(unused_imports)] +#![allow( + unused_imports, + clippy::cognitive_complexity + )] #![warn(clippy::explicit_write)] fn stdout() -> String { diff --git a/tests/ui/explicit_write.stderr b/tests/ui/explicit_write.stderr index 9feef9c0dc84..29d6aa8bb4e2 100644 --- a/tests/ui/explicit_write.stderr +++ b/tests/ui/explicit_write.stderr @@ -1,5 +1,5 @@ error: use of `write!(stdout(), ...).unwrap()` - --> $DIR/explicit_write.rs:17:9 + --> $DIR/explicit_write.rs:20:9 | LL | write!(std::io::stdout(), "test").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `print!("test")` @@ -7,43 +7,43 @@ LL | write!(std::io::stdout(), "test").unwrap(); = note: `-D clippy::explicit-write` implied by `-D warnings` error: use of `write!(stderr(), ...).unwrap()` - --> $DIR/explicit_write.rs:18:9 + --> $DIR/explicit_write.rs:21:9 | LL | write!(std::io::stderr(), "test").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprint!("test")` error: use of `writeln!(stdout(), ...).unwrap()` - --> $DIR/explicit_write.rs:19:9 + --> $DIR/explicit_write.rs:22:9 | LL | writeln!(std::io::stdout(), "test").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `println!("test")` error: use of `writeln!(stderr(), ...).unwrap()` - --> $DIR/explicit_write.rs:20:9 + --> $DIR/explicit_write.rs:23:9 | LL | writeln!(std::io::stderr(), "test").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test")` error: use of `stdout().write_fmt(...).unwrap()` - --> $DIR/explicit_write.rs:21:9 + --> $DIR/explicit_write.rs:24:9 | LL | std::io::stdout().write_fmt(format_args!("test")).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `print!("test")` error: use of `stderr().write_fmt(...).unwrap()` - --> $DIR/explicit_write.rs:22:9 + --> $DIR/explicit_write.rs:25:9 | LL | std::io::stderr().write_fmt(format_args!("test")).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprint!("test")` error: use of `writeln!(stdout(), ...).unwrap()` - --> $DIR/explicit_write.rs:25:9 + --> $DIR/explicit_write.rs:28:9 | LL | writeln!(std::io::stdout(), "test/ntest").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `println!("test/ntest")` error: use of `writeln!(stderr(), ...).unwrap()` - --> $DIR/explicit_write.rs:26:9 + --> $DIR/explicit_write.rs:29:9 | LL | writeln!(std::io::stderr(), "test/ntest").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test/ntest")` diff --git a/tests/ui/formatting.rs b/tests/ui/formatting.rs index 149f1da93966..8e0b27a9619a 100644 --- a/tests/ui/formatting.rs +++ b/tests/ui/formatting.rs @@ -1,8 +1,11 @@ #![warn(clippy::all)] -#![allow(unused_variables)] -#![allow(unused_assignments)] -#![allow(clippy::if_same_then_else)] -#![allow(clippy::deref_addrof)] +#![allow( + unused_variables, + unused_assignments, + clippy::cognitive_complexity, + clippy::if_same_then_else, + clippy::deref_addrof + )] fn foo() -> bool { true diff --git a/tests/ui/formatting.stderr b/tests/ui/formatting.stderr index 29a5c55bc34b..a1a81cd8d011 100644 --- a/tests/ui/formatting.stderr +++ b/tests/ui/formatting.stderr @@ -1,5 +1,5 @@ error: this looks like an `else {..}` but the `else` is missing - --> $DIR/formatting.rs:15:6 + --> $DIR/formatting.rs:18:6 | LL | } { | ^ @@ -8,7 +8,7 @@ LL | } { = note: to remove this lint, add the missing `else` or add a new line before the next block error: this looks like an `else if` but the `else` is missing - --> $DIR/formatting.rs:19:6 + --> $DIR/formatting.rs:22:6 | LL | } if foo() { | ^ @@ -16,7 +16,7 @@ LL | } if foo() { = note: to remove this lint, add the missing `else` or add a new line before the second `if` error: this looks like an `else if` but the `else` is missing - --> $DIR/formatting.rs:26:10 + --> $DIR/formatting.rs:29:10 | LL | } if foo() { | ^ @@ -24,7 +24,7 @@ LL | } if foo() { = note: to remove this lint, add the missing `else` or add a new line before the second `if` error: this looks like an `else if` but the `else` is missing - --> $DIR/formatting.rs:34:10 + --> $DIR/formatting.rs:37:10 | LL | } if foo() { | ^ @@ -32,7 +32,7 @@ LL | } if foo() { = note: to remove this lint, add the missing `else` or add a new line before the second `if` error: this is an `else {..}` but the formatting might hide it - --> $DIR/formatting.rs:43:6 + --> $DIR/formatting.rs:46:6 | LL | } else | ______^ @@ -42,7 +42,7 @@ LL | | { = note: to remove this lint, remove the `else` or remove the new line between `else` and `{..}` error: this is an `else {..}` but the formatting might hide it - --> $DIR/formatting.rs:48:6 + --> $DIR/formatting.rs:51:6 | LL | } | ______^ @@ -53,7 +53,7 @@ LL | | { = note: to remove this lint, remove the `else` or remove the new line between `else` and `{..}` error: this is an `else if` but the formatting might hide it - --> $DIR/formatting.rs:54:6 + --> $DIR/formatting.rs:57:6 | LL | } else | ______^ @@ -63,7 +63,7 @@ LL | | if foo() { // the span of the above error should continue here = note: to remove this lint, remove the `else` or remove the new line between `else` and `if` error: this is an `else if` but the formatting might hide it - --> $DIR/formatting.rs:59:6 + --> $DIR/formatting.rs:62:6 | LL | } | ______^ @@ -74,7 +74,7 @@ LL | | if foo() { // the span of the above error should continue here = note: to remove this lint, remove the `else` or remove the new line between `else` and `if` error: this looks like you are trying to use `.. -= ..`, but you really are doing `.. = (- ..)` - --> $DIR/formatting.rs:100:6 + --> $DIR/formatting.rs:103:6 | LL | a =- 35; | ^^^^ @@ -83,7 +83,7 @@ LL | a =- 35; = note: to remove this lint, use either `-=` or `= -` error: this looks like you are trying to use `.. *= ..`, but you really are doing `.. = (* ..)` - --> $DIR/formatting.rs:101:6 + --> $DIR/formatting.rs:104:6 | LL | a =* &191; | ^^^^ @@ -91,7 +91,7 @@ LL | a =* &191; = note: to remove this lint, use either `*=` or `= *` error: this looks like you are trying to use `.. != ..`, but you really are doing `.. = (! ..)` - --> $DIR/formatting.rs:104:6 + --> $DIR/formatting.rs:107:6 | LL | b =! false; | ^^^^ @@ -99,7 +99,7 @@ LL | b =! false; = note: to remove this lint, use either `!=` or `= !` error: possibly missing a comma here - --> $DIR/formatting.rs:113:19 + --> $DIR/formatting.rs:116:19 | LL | -1, -2, -3 // <= no comma here | ^ @@ -108,7 +108,7 @@ LL | -1, -2, -3 // <= no comma here = note: to remove this lint, add a comma or write the expr in a single line error: possibly missing a comma here - --> $DIR/formatting.rs:117:19 + --> $DIR/formatting.rs:120:19 | LL | -1, -2, -3 // <= no comma here | ^ diff --git a/tests/ui/functions_maxlines.rs b/tests/ui/functions_maxlines.rs index ada35abde99c..ac015a7c85e9 100644 --- a/tests/ui/functions_maxlines.rs +++ b/tests/ui/functions_maxlines.rs @@ -1,4 +1,5 @@ #![warn(clippy::too_many_lines)] +#![allow(clippy::cognitive_complexity)] fn good_lines() { /* println!("This is good."); */ diff --git a/tests/ui/functions_maxlines.stderr b/tests/ui/functions_maxlines.stderr index dfa6a1cf3c5f..9e1b2fe568a4 100644 --- a/tests/ui/functions_maxlines.stderr +++ b/tests/ui/functions_maxlines.stderr @@ -1,5 +1,5 @@ error: This function has a large number of lines. - --> $DIR/functions_maxlines.rs:58:1 + --> $DIR/functions_maxlines.rs:59:1 | LL | / fn bad_lines() { LL | | println!("This is bad."); diff --git a/tests/ui/len_zero.rs b/tests/ui/len_zero.rs index db97c2427f37..75a78800a543 100644 --- a/tests/ui/len_zero.rs +++ b/tests/ui/len_zero.rs @@ -1,5 +1,9 @@ #![warn(clippy::len_without_is_empty, clippy::len_zero)] -#![allow(dead_code, unused)] +#![allow( + dead_code, + unused, + clippy::cognitive_complexity + )] pub struct PubOne; diff --git a/tests/ui/len_zero.stderr b/tests/ui/len_zero.stderr index f2ad0bb9e1e4..8571aeebfee7 100644 --- a/tests/ui/len_zero.stderr +++ b/tests/ui/len_zero.stderr @@ -1,5 +1,5 @@ error: item `PubOne` has a public `len` method but no corresponding `is_empty` method - --> $DIR/len_zero.rs:6:1 + --> $DIR/len_zero.rs:10:1 | LL | / impl PubOne { LL | | pub fn len(self: &Self) -> isize { @@ -11,7 +11,7 @@ LL | | } = note: `-D clippy::len-without-is-empty` implied by `-D warnings` error: trait `PubTraitsToo` has a `len` method but no (possibly inherited) `is_empty` method - --> $DIR/len_zero.rs:55:1 + --> $DIR/len_zero.rs:59:1 | LL | / pub trait PubTraitsToo { LL | | fn len(self: &Self) -> isize; @@ -19,7 +19,7 @@ LL | | } | |_^ error: item `HasIsEmpty` has a public `len` method but a private `is_empty` method - --> $DIR/len_zero.rs:90:1 + --> $DIR/len_zero.rs:94:1 | LL | / impl HasIsEmpty { LL | | pub fn len(self: &Self) -> isize { @@ -31,7 +31,7 @@ LL | | } | |_^ error: item `HasWrongIsEmpty` has a public `len` method but no corresponding `is_empty` method - --> $DIR/len_zero.rs:119:1 + --> $DIR/len_zero.rs:123:1 | LL | / impl HasWrongIsEmpty { LL | | pub fn len(self: &Self) -> isize { @@ -43,7 +43,7 @@ LL | | } | |_^ error: length comparison to zero - --> $DIR/len_zero.rs:140:8 + --> $DIR/len_zero.rs:144:8 | LL | if x.len() == 0 { | ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `x.is_empty()` @@ -51,85 +51,85 @@ LL | if x.len() == 0 { = note: `-D clippy::len-zero` implied by `-D warnings` error: length comparison to zero - --> $DIR/len_zero.rs:144:8 + --> $DIR/len_zero.rs:148:8 | LL | if "".len() == 0 {} | ^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `"".is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:159:8 + --> $DIR/len_zero.rs:163:8 | LL | if has_is_empty.len() == 0 { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:162:8 + --> $DIR/len_zero.rs:166:8 | LL | if has_is_empty.len() != 0 { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:165:8 + --> $DIR/len_zero.rs:169:8 | LL | if has_is_empty.len() > 0 { | ^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to one - --> $DIR/len_zero.rs:168:8 + --> $DIR/len_zero.rs:172:8 | LL | if has_is_empty.len() < 1 { | ^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()` error: length comparison to one - --> $DIR/len_zero.rs:171:8 + --> $DIR/len_zero.rs:175:8 | LL | if has_is_empty.len() >= 1 { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:182:8 + --> $DIR/len_zero.rs:186:8 | LL | if 0 == has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:185:8 + --> $DIR/len_zero.rs:189:8 | LL | if 0 != has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:188:8 + --> $DIR/len_zero.rs:192:8 | LL | if 0 < has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to one - --> $DIR/len_zero.rs:191:8 + --> $DIR/len_zero.rs:195:8 | LL | if 1 <= has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to one - --> $DIR/len_zero.rs:194:8 + --> $DIR/len_zero.rs:198:8 | LL | if 1 > has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:208:8 + --> $DIR/len_zero.rs:212:8 | LL | if with_is_empty.len() == 0 { | ^^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `with_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:221:8 + --> $DIR/len_zero.rs:225:8 | LL | if b.len() != 0 {} | ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `!b.is_empty()` error: trait `DependsOnFoo` has a `len` method but no (possibly inherited) `is_empty` method - --> $DIR/len_zero.rs:227:1 + --> $DIR/len_zero.rs:231:1 | LL | / pub trait DependsOnFoo: Foo { LL | | fn len(&mut self) -> usize; diff --git a/tests/ui/matches.rs b/tests/ui/matches.rs index b711367d123c..dcefd22355ba 100644 --- a/tests/ui/matches.rs +++ b/tests/ui/matches.rs @@ -1,6 +1,11 @@ #![feature(exclusive_range_pattern)] #![warn(clippy::all)] -#![allow(unused, clippy::redundant_pattern_matching, clippy::too_many_lines)] +#![allow( + unused, + clippy::redundant_pattern_matching, + clippy::too_many_lines, + clippy::cognitive_complexity + )] #![warn(clippy::match_same_arms)] fn dummy() {} diff --git a/tests/ui/matches.stderr b/tests/ui/matches.stderr index b4159f7a68d0..65a87f962725 100644 --- a/tests/ui/matches.stderr +++ b/tests/ui/matches.stderr @@ -1,5 +1,5 @@ error: you don't need to add `&` to all patterns - --> $DIR/matches.rs:11:9 + --> $DIR/matches.rs:16:9 | LL | / match v { LL | | &Some(v) => println!("{:?}", v), @@ -16,7 +16,7 @@ LL | None => println!("none"), | error: you don't need to add `&` to all patterns - --> $DIR/matches.rs:22:5 + --> $DIR/matches.rs:27:5 | LL | / match tup { LL | | &(v, 1) => println!("{}", v), @@ -30,7 +30,7 @@ LL | (v, 1) => println!("{}", v), | error: you don't need to add `&` to both the expression and the patterns - --> $DIR/matches.rs:28:5 + --> $DIR/matches.rs:33:5 | LL | / match &w { LL | | &Some(v) => println!("{:?}", v), @@ -45,7 +45,7 @@ LL | None => println!("none"), | error: you don't need to add `&` to all patterns - --> $DIR/matches.rs:39:5 + --> $DIR/matches.rs:44:5 | LL | / if let &None = a { LL | | println!("none"); @@ -57,7 +57,7 @@ LL | if let None = *a { | ^^^^ ^^ error: you don't need to add `&` to both the expression and the patterns - --> $DIR/matches.rs:44:5 + --> $DIR/matches.rs:49:5 | LL | / if let &None = &b { LL | | println!("none"); @@ -69,7 +69,7 @@ LL | if let None = b { | ^^^^ ^ error: Err(_) will match all errors, maybe not a good idea - --> $DIR/matches.rs:55:9 + --> $DIR/matches.rs:60:9 | LL | Err(_) => panic!("err"), | ^^^^^^ @@ -78,26 +78,26 @@ LL | Err(_) => panic!("err"), = note: to remove this warning, match each error separately or use unreachable macro error: this `match` has identical arm bodies - --> $DIR/matches.rs:54:18 + --> $DIR/matches.rs:59:18 | LL | Ok(_) => println!("ok"), | ^^^^^^^^^^^^^^ | = note: `-D clippy::match-same-arms` implied by `-D warnings` note: same as this - --> $DIR/matches.rs:53:18 + --> $DIR/matches.rs:58:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ note: consider refactoring into `Ok(3) | Ok(_)` - --> $DIR/matches.rs:53:18 + --> $DIR/matches.rs:58:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: Err(_) will match all errors, maybe not a good idea - --> $DIR/matches.rs:61:9 + --> $DIR/matches.rs:66:9 | LL | Err(_) => panic!(), | ^^^^^^ @@ -105,25 +105,25 @@ LL | Err(_) => panic!(), = note: to remove this warning, match each error separately or use unreachable macro error: this `match` has identical arm bodies - --> $DIR/matches.rs:60:18 + --> $DIR/matches.rs:65:18 | LL | Ok(_) => println!("ok"), | ^^^^^^^^^^^^^^ | note: same as this - --> $DIR/matches.rs:59:18 + --> $DIR/matches.rs:64:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ note: consider refactoring into `Ok(3) | Ok(_)` - --> $DIR/matches.rs:59:18 + --> $DIR/matches.rs:64:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: Err(_) will match all errors, maybe not a good idea - --> $DIR/matches.rs:67:9 + --> $DIR/matches.rs:72:9 | LL | Err(_) => { | ^^^^^^ @@ -131,133 +131,133 @@ LL | Err(_) => { = note: to remove this warning, match each error separately or use unreachable macro error: this `match` has identical arm bodies - --> $DIR/matches.rs:66:18 + --> $DIR/matches.rs:71:18 | LL | Ok(_) => println!("ok"), | ^^^^^^^^^^^^^^ | note: same as this - --> $DIR/matches.rs:65:18 + --> $DIR/matches.rs:70:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ note: consider refactoring into `Ok(3) | Ok(_)` - --> $DIR/matches.rs:65:18 + --> $DIR/matches.rs:70:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: this `match` has identical arm bodies - --> $DIR/matches.rs:75:18 + --> $DIR/matches.rs:80:18 | LL | Ok(_) => println!("ok"), | ^^^^^^^^^^^^^^ | note: same as this - --> $DIR/matches.rs:74:18 + --> $DIR/matches.rs:79:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ note: consider refactoring into `Ok(3) | Ok(_)` - --> $DIR/matches.rs:74:18 + --> $DIR/matches.rs:79:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: this `match` has identical arm bodies - --> $DIR/matches.rs:82:18 + --> $DIR/matches.rs:87:18 | LL | Ok(_) => println!("ok"), | ^^^^^^^^^^^^^^ | note: same as this - --> $DIR/matches.rs:81:18 + --> $DIR/matches.rs:86:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ note: consider refactoring into `Ok(3) | Ok(_)` - --> $DIR/matches.rs:81:18 + --> $DIR/matches.rs:86:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: this `match` has identical arm bodies - --> $DIR/matches.rs:88:18 + --> $DIR/matches.rs:93:18 | LL | Ok(_) => println!("ok"), | ^^^^^^^^^^^^^^ | note: same as this - --> $DIR/matches.rs:87:18 + --> $DIR/matches.rs:92:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ note: consider refactoring into `Ok(3) | Ok(_)` - --> $DIR/matches.rs:87:18 + --> $DIR/matches.rs:92:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: this `match` has identical arm bodies - --> $DIR/matches.rs:94:18 + --> $DIR/matches.rs:99:18 | LL | Ok(_) => println!("ok"), | ^^^^^^^^^^^^^^ | note: same as this - --> $DIR/matches.rs:93:18 + --> $DIR/matches.rs:98:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ note: consider refactoring into `Ok(3) | Ok(_)` - --> $DIR/matches.rs:93:18 + --> $DIR/matches.rs:98:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: this `match` has identical arm bodies - --> $DIR/matches.rs:117:29 + --> $DIR/matches.rs:122:29 | LL | (Ok(_), Some(x)) => println!("ok {}", x), | ^^^^^^^^^^^^^^^^^^^^ | note: same as this - --> $DIR/matches.rs:116:29 + --> $DIR/matches.rs:121:29 | LL | (Ok(x), Some(_)) => println!("ok {}", x), | ^^^^^^^^^^^^^^^^^^^^ note: consider refactoring into `(Ok(x), Some(_)) | (Ok(_), Some(x))` - --> $DIR/matches.rs:116:29 + --> $DIR/matches.rs:121:29 | LL | (Ok(x), Some(_)) => println!("ok {}", x), | ^^^^^^^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: this `match` has identical arm bodies - --> $DIR/matches.rs:132:18 + --> $DIR/matches.rs:137:18 | LL | Ok(_) => println!("ok"), | ^^^^^^^^^^^^^^ | note: same as this - --> $DIR/matches.rs:131:18 + --> $DIR/matches.rs:136:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ note: consider refactoring into `Ok(3) | Ok(_)` - --> $DIR/matches.rs:131:18 + --> $DIR/matches.rs:136:18 | LL | Ok(3) => println!("ok"), | ^^^^^^^^^^^^^^ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: use as_ref() instead - --> $DIR/matches.rs:141:33 + --> $DIR/matches.rs:146:33 | LL | let borrowed: Option<&()> = match owned { | _________________________________^ @@ -269,7 +269,7 @@ LL | | }; = note: `-D clippy::match-as-ref` implied by `-D warnings` error: use as_mut() instead - --> $DIR/matches.rs:147:39 + --> $DIR/matches.rs:152:39 | LL | let borrow_mut: Option<&mut ()> = match mut_owned { | _______________________________________^ @@ -279,7 +279,7 @@ LL | | }; | |_____^ help: try this: `mut_owned.as_mut()` error: you don't need to add `&` to all patterns - --> $DIR/matches.rs:174:5 + --> $DIR/matches.rs:179:5 | LL | / match foo_variant!(0) { LL | | &Foo::A => println!("A"), diff --git a/tests/ui/option_map_unit_fn.rs b/tests/ui/option_map_unit_fn.rs index 1d2a3a17ee00..4e7fb5cc386c 100644 --- a/tests/ui/option_map_unit_fn.rs +++ b/tests/ui/option_map_unit_fn.rs @@ -1,5 +1,8 @@ #![warn(clippy::option_map_unit_fn)] -#![allow(unused)] +#![allow( + unused, + clippy::cognitive_complexity + )] fn do_nothing(_: T) {} diff --git a/tests/ui/option_map_unit_fn.stderr b/tests/ui/option_map_unit_fn.stderr index 16e355ad0b2d..c3415c858448 100644 --- a/tests/ui/option_map_unit_fn.stderr +++ b/tests/ui/option_map_unit_fn.stderr @@ -1,5 +1,5 @@ error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn.rs:32:5 + --> $DIR/option_map_unit_fn.rs:35:5 | LL | x.field.map(do_nothing); | ^^^^^^^^^^^^^^^^^^^^^^^- @@ -9,7 +9,7 @@ LL | x.field.map(do_nothing); = note: `-D clippy::option-map-unit-fn` implied by `-D warnings` error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn.rs:34:5 + --> $DIR/option_map_unit_fn.rs:37:5 | LL | x.field.map(do_nothing); | ^^^^^^^^^^^^^^^^^^^^^^^- @@ -17,7 +17,7 @@ LL | x.field.map(do_nothing); | help: try this: `if let Some(x_field) = x.field { do_nothing(...) }` error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn.rs:36:5 + --> $DIR/option_map_unit_fn.rs:39:5 | LL | x.field.map(diverge); | ^^^^^^^^^^^^^^^^^^^^- @@ -25,7 +25,7 @@ LL | x.field.map(diverge); | help: try this: `if let Some(x_field) = x.field { diverge(...) }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:42:5 + --> $DIR/option_map_unit_fn.rs:45:5 | LL | x.field.map(|value| x.do_option_nothing(value + captured)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -33,7 +33,7 @@ LL | x.field.map(|value| x.do_option_nothing(value + captured)); | help: try this: `if let Some(value) = x.field { x.do_option_nothing(value + captured) }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:44:5 + --> $DIR/option_map_unit_fn.rs:47:5 | LL | x.field.map(|value| { x.do_option_plus_one(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -41,7 +41,7 @@ LL | x.field.map(|value| { x.do_option_plus_one(value + captured); }); | help: try this: `if let Some(value) = x.field { x.do_option_plus_one(value + captured); }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:47:5 + --> $DIR/option_map_unit_fn.rs:50:5 | LL | x.field.map(|value| do_nothing(value + captured)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -49,7 +49,7 @@ LL | x.field.map(|value| do_nothing(value + captured)); | help: try this: `if let Some(value) = x.field { do_nothing(value + captured) }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:49:5 + --> $DIR/option_map_unit_fn.rs:52:5 | LL | x.field.map(|value| { do_nothing(value + captured) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -57,7 +57,7 @@ LL | x.field.map(|value| { do_nothing(value + captured) }); | help: try this: `if let Some(value) = x.field { do_nothing(value + captured) }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:51:5 + --> $DIR/option_map_unit_fn.rs:54:5 | LL | x.field.map(|value| { do_nothing(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -65,7 +65,7 @@ LL | x.field.map(|value| { do_nothing(value + captured); }); | help: try this: `if let Some(value) = x.field { do_nothing(value + captured); }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:53:5 + --> $DIR/option_map_unit_fn.rs:56:5 | LL | x.field.map(|value| { { do_nothing(value + captured); } }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -73,7 +73,7 @@ LL | x.field.map(|value| { { do_nothing(value + captured); } }); | help: try this: `if let Some(value) = x.field { do_nothing(value + captured); }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:56:5 + --> $DIR/option_map_unit_fn.rs:59:5 | LL | x.field.map(|value| diverge(value + captured)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -81,7 +81,7 @@ LL | x.field.map(|value| diverge(value + captured)); | help: try this: `if let Some(value) = x.field { diverge(value + captured) }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:58:5 + --> $DIR/option_map_unit_fn.rs:61:5 | LL | x.field.map(|value| { diverge(value + captured) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -89,7 +89,7 @@ LL | x.field.map(|value| { diverge(value + captured) }); | help: try this: `if let Some(value) = x.field { diverge(value + captured) }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:60:5 + --> $DIR/option_map_unit_fn.rs:63:5 | LL | x.field.map(|value| { diverge(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -97,7 +97,7 @@ LL | x.field.map(|value| { diverge(value + captured); }); | help: try this: `if let Some(value) = x.field { diverge(value + captured); }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:62:5 + --> $DIR/option_map_unit_fn.rs:65:5 | LL | x.field.map(|value| { { diverge(value + captured); } }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -105,7 +105,7 @@ LL | x.field.map(|value| { { diverge(value + captured); } }); | help: try this: `if let Some(value) = x.field { diverge(value + captured); }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:67:5 + --> $DIR/option_map_unit_fn.rs:70:5 | LL | x.field.map(|value| { let y = plus_one(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -113,7 +113,7 @@ LL | x.field.map(|value| { let y = plus_one(value + captured); }); | help: try this: `if let Some(value) = x.field { let y = plus_one(value + captured); }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:69:5 + --> $DIR/option_map_unit_fn.rs:72:5 | LL | x.field.map(|value| { plus_one(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -121,7 +121,7 @@ LL | x.field.map(|value| { plus_one(value + captured); }); | help: try this: `if let Some(value) = x.field { plus_one(value + captured); }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:71:5 + --> $DIR/option_map_unit_fn.rs:74:5 | LL | x.field.map(|value| { { plus_one(value + captured); } }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -129,7 +129,7 @@ LL | x.field.map(|value| { { plus_one(value + captured); } }); | help: try this: `if let Some(value) = x.field { plus_one(value + captured); }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:74:5 + --> $DIR/option_map_unit_fn.rs:77:5 | LL | x.field.map(|ref value| { do_nothing(value + captured) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -137,7 +137,7 @@ LL | x.field.map(|ref value| { do_nothing(value + captured) }); | help: try this: `if let Some(ref value) = x.field { do_nothing(value + captured) }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:77:5 + --> $DIR/option_map_unit_fn.rs:80:5 | LL | x.field.map(|value| { do_nothing(value); do_nothing(value) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -145,7 +145,7 @@ LL | x.field.map(|value| { do_nothing(value); do_nothing(value) }); | help: try this: `if let Some(value) = x.field { ... }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:79:5 + --> $DIR/option_map_unit_fn.rs:82:5 | LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -153,7 +153,7 @@ LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) | help: try this: `if let Some(value) = x.field { ... }` error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:83:5 + --> $DIR/option_map_unit_fn.rs:86:5 | LL | x.field.map(|value| { | _____^ @@ -167,7 +167,7 @@ LL | || }); | error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn.rs:87:5 + --> $DIR/option_map_unit_fn.rs:90:5 | LL | x.field.map(|value| { do_nothing(value); do_nothing(value); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -175,7 +175,7 @@ LL | x.field.map(|value| { do_nothing(value); do_nothing(value); }); | help: try this: `if let Some(value) = x.field { ... }` error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn.rs:90:5 + --> $DIR/option_map_unit_fn.rs:93:5 | LL | Some(42).map(diverge); | ^^^^^^^^^^^^^^^^^^^^^- @@ -183,7 +183,7 @@ LL | Some(42).map(diverge); | help: try this: `if let Some(_) = Some(42) { diverge(...) }` error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn.rs:91:5 + --> $DIR/option_map_unit_fn.rs:94:5 | LL | "12".parse::().ok().map(diverge); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -191,7 +191,7 @@ LL | "12".parse::().ok().map(diverge); | help: try this: `if let Some(_) = "12".parse::().ok() { diverge(...) }` error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn.rs:92:5 + --> $DIR/option_map_unit_fn.rs:95:5 | LL | Some(plus_one(1)).map(do_nothing); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -199,7 +199,7 @@ LL | Some(plus_one(1)).map(do_nothing); | help: try this: `if let Some(_) = Some(plus_one(1)) { do_nothing(...) }` error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn.rs:96:5 + --> $DIR/option_map_unit_fn.rs:99:5 | LL | y.map(do_nothing); | ^^^^^^^^^^^^^^^^^- diff --git a/tests/ui/or_fun_call.rs b/tests/ui/or_fun_call.rs index 562120c3ea0f..d9ed20b6a278 100644 --- a/tests/ui/or_fun_call.rs +++ b/tests/ui/or_fun_call.rs @@ -1,7 +1,7 @@ #![warn(clippy::or_fun_call)] +#![allow(clippy::cognitive_complexity)] -use std::collections::BTreeMap; -use std::collections::HashMap; +use std::collections::{BTreeMap,HashMap}; /// Checks implementation of the `OR_FUN_CALL` lint. fn or_fun_call() { diff --git a/tests/ui/result_map_unit_fn.rs b/tests/ui/result_map_unit_fn.rs index a8e891d8db02..429f743821b9 100644 --- a/tests/ui/result_map_unit_fn.rs +++ b/tests/ui/result_map_unit_fn.rs @@ -1,6 +1,9 @@ #![feature(never_type)] #![warn(clippy::result_map_unit_fn)] -#![allow(unused)] +#![allow( + unused, + clippy::cognitive_complexity + )] fn do_nothing(_: T) {} diff --git a/tests/ui/result_map_unit_fn.stderr b/tests/ui/result_map_unit_fn.stderr index 9f9025152e24..5f6a2785c95d 100644 --- a/tests/ui/result_map_unit_fn.stderr +++ b/tests/ui/result_map_unit_fn.stderr @@ -1,5 +1,5 @@ error: called `map(f)` on an Result value where `f` is a unit function - --> $DIR/result_map_unit_fn.rs:34:5 + --> $DIR/result_map_unit_fn.rs:37:5 | LL | x.field.map(do_nothing); | ^^^^^^^^^^^^^^^^^^^^^^^- @@ -9,7 +9,7 @@ LL | x.field.map(do_nothing); = note: `-D clippy::result-map-unit-fn` implied by `-D warnings` error: called `map(f)` on an Result value where `f` is a unit function - --> $DIR/result_map_unit_fn.rs:36:5 + --> $DIR/result_map_unit_fn.rs:39:5 | LL | x.field.map(do_nothing); | ^^^^^^^^^^^^^^^^^^^^^^^- @@ -17,7 +17,7 @@ LL | x.field.map(do_nothing); | help: try this: `if let Ok(x_field) = x.field { do_nothing(...) }` error: called `map(f)` on an Result value where `f` is a unit function - --> $DIR/result_map_unit_fn.rs:38:5 + --> $DIR/result_map_unit_fn.rs:41:5 | LL | x.field.map(diverge); | ^^^^^^^^^^^^^^^^^^^^- @@ -25,7 +25,7 @@ LL | x.field.map(diverge); | help: try this: `if let Ok(x_field) = x.field { diverge(...) }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:44:5 + --> $DIR/result_map_unit_fn.rs:47:5 | LL | x.field.map(|value| x.do_result_nothing(value + captured)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -33,7 +33,7 @@ LL | x.field.map(|value| x.do_result_nothing(value + captured)); | help: try this: `if let Ok(value) = x.field { x.do_result_nothing(value + captured) }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:46:5 + --> $DIR/result_map_unit_fn.rs:49:5 | LL | x.field.map(|value| { x.do_result_plus_one(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -41,7 +41,7 @@ LL | x.field.map(|value| { x.do_result_plus_one(value + captured); }); | help: try this: `if let Ok(value) = x.field { x.do_result_plus_one(value + captured); }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:49:5 + --> $DIR/result_map_unit_fn.rs:52:5 | LL | x.field.map(|value| do_nothing(value + captured)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -49,7 +49,7 @@ LL | x.field.map(|value| do_nothing(value + captured)); | help: try this: `if let Ok(value) = x.field { do_nothing(value + captured) }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:51:5 + --> $DIR/result_map_unit_fn.rs:54:5 | LL | x.field.map(|value| { do_nothing(value + captured) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -57,7 +57,7 @@ LL | x.field.map(|value| { do_nothing(value + captured) }); | help: try this: `if let Ok(value) = x.field { do_nothing(value + captured) }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:53:5 + --> $DIR/result_map_unit_fn.rs:56:5 | LL | x.field.map(|value| { do_nothing(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -65,7 +65,7 @@ LL | x.field.map(|value| { do_nothing(value + captured); }); | help: try this: `if let Ok(value) = x.field { do_nothing(value + captured); }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:55:5 + --> $DIR/result_map_unit_fn.rs:58:5 | LL | x.field.map(|value| { { do_nothing(value + captured); } }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -73,7 +73,7 @@ LL | x.field.map(|value| { { do_nothing(value + captured); } }); | help: try this: `if let Ok(value) = x.field { do_nothing(value + captured); }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:58:5 + --> $DIR/result_map_unit_fn.rs:61:5 | LL | x.field.map(|value| diverge(value + captured)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -81,7 +81,7 @@ LL | x.field.map(|value| diverge(value + captured)); | help: try this: `if let Ok(value) = x.field { diverge(value + captured) }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:60:5 + --> $DIR/result_map_unit_fn.rs:63:5 | LL | x.field.map(|value| { diverge(value + captured) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -89,7 +89,7 @@ LL | x.field.map(|value| { diverge(value + captured) }); | help: try this: `if let Ok(value) = x.field { diverge(value + captured) }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:62:5 + --> $DIR/result_map_unit_fn.rs:65:5 | LL | x.field.map(|value| { diverge(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -97,7 +97,7 @@ LL | x.field.map(|value| { diverge(value + captured); }); | help: try this: `if let Ok(value) = x.field { diverge(value + captured); }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:64:5 + --> $DIR/result_map_unit_fn.rs:67:5 | LL | x.field.map(|value| { { diverge(value + captured); } }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -105,7 +105,7 @@ LL | x.field.map(|value| { { diverge(value + captured); } }); | help: try this: `if let Ok(value) = x.field { diverge(value + captured); }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:69:5 + --> $DIR/result_map_unit_fn.rs:72:5 | LL | x.field.map(|value| { let y = plus_one(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -113,7 +113,7 @@ LL | x.field.map(|value| { let y = plus_one(value + captured); }); | help: try this: `if let Ok(value) = x.field { let y = plus_one(value + captured); }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:71:5 + --> $DIR/result_map_unit_fn.rs:74:5 | LL | x.field.map(|value| { plus_one(value + captured); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -121,7 +121,7 @@ LL | x.field.map(|value| { plus_one(value + captured); }); | help: try this: `if let Ok(value) = x.field { plus_one(value + captured); }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:73:5 + --> $DIR/result_map_unit_fn.rs:76:5 | LL | x.field.map(|value| { { plus_one(value + captured); } }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -129,7 +129,7 @@ LL | x.field.map(|value| { { plus_one(value + captured); } }); | help: try this: `if let Ok(value) = x.field { plus_one(value + captured); }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:76:5 + --> $DIR/result_map_unit_fn.rs:79:5 | LL | x.field.map(|ref value| { do_nothing(value + captured) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -137,7 +137,7 @@ LL | x.field.map(|ref value| { do_nothing(value + captured) }); | help: try this: `if let Ok(ref value) = x.field { do_nothing(value + captured) }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:79:5 + --> $DIR/result_map_unit_fn.rs:82:5 | LL | x.field.map(|value| { do_nothing(value); do_nothing(value) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -145,7 +145,7 @@ LL | x.field.map(|value| { do_nothing(value); do_nothing(value) }); | help: try this: `if let Ok(value) = x.field { ... }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:81:5 + --> $DIR/result_map_unit_fn.rs:84:5 | LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -153,7 +153,7 @@ LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) | help: try this: `if let Ok(value) = x.field { ... }` error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:85:5 + --> $DIR/result_map_unit_fn.rs:88:5 | LL | x.field.map(|value| { | _____^ @@ -167,7 +167,7 @@ LL | || }); | error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn.rs:89:5 + --> $DIR/result_map_unit_fn.rs:92:5 | LL | x.field.map(|value| { do_nothing(value); do_nothing(value); }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -175,7 +175,7 @@ LL | x.field.map(|value| { do_nothing(value); do_nothing(value); }); | help: try this: `if let Ok(value) = x.field { ... }` error: called `map(f)` on an Result value where `f` is a unit function - --> $DIR/result_map_unit_fn.rs:93:5 + --> $DIR/result_map_unit_fn.rs:96:5 | LL | "12".parse::().map(diverge); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- @@ -183,7 +183,7 @@ LL | "12".parse::().map(diverge); | help: try this: `if let Ok(_) = "12".parse::() { diverge(...) }` error: called `map(f)` on an Result value where `f` is a unit function - --> $DIR/result_map_unit_fn.rs:99:5 + --> $DIR/result_map_unit_fn.rs:102:5 | LL | y.map(do_nothing); | ^^^^^^^^^^^^^^^^^-