From 973d676cd105b3ec87e4edb99fc0fb63b374e213 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Thu, 6 Dec 2018 12:22:54 +0100 Subject: [PATCH 1/3] Fix bug in `implicit_return`. Bug was already covered by test, but test was not checked for. --- clippy_lints/src/implicit_return.rs | 6 ++++++ tests/ui/implicit_return.stderr | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/implicit_return.rs b/clippy_lints/src/implicit_return.rs index 664f182c533..d29b508ba37 100644 --- a/clippy_lints/src/implicit_return.rs +++ b/clippy_lints/src/implicit_return.rs @@ -90,6 +90,12 @@ impl Pass { if let Some(expr) = &block.expr { Self::expr_match(cx, expr); } + // only needed in the case of `break` with `;` at the end + else if let Some(stmt) = block.stmts.last() { + if let rustc::hir::StmtKind::Semi(expr, ..) = &stmt.node { + Self::expr_match(cx, expr); + } + } }, // skip if it already has a return statement ExprKind::Ret(..) => (), diff --git a/tests/ui/implicit_return.stderr b/tests/ui/implicit_return.stderr index bba8d942e27..6f4fe12757a 100644 --- a/tests/ui/implicit_return.stderr +++ b/tests/ui/implicit_return.stderr @@ -30,6 +30,12 @@ error: missing return statement 38 | true | ^^^^ help: add `return` as shown: `return true` +error: missing return statement + --> $DIR/implicit_return.rs:46:9 + | +46 | break true; + | ^^^^^^^^^^ help: change `break` to `return` as shown: `return true` + error: missing return statement --> $DIR/implicit_return.rs:52:9 | @@ -42,5 +48,5 @@ error: missing return statement 54 | let _ = || true; | ^^^^ help: add `return` as shown: `return true` -error: aborting due to 7 previous errors +error: aborting due to 8 previous errors From d048e15835e19841211ae7725511dc946bdde5e2 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Thu, 6 Dec 2018 13:21:04 +0100 Subject: [PATCH 2/3] Improved code noted by clippy. --- clippy_lints/src/implicit_return.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/implicit_return.rs b/clippy_lints/src/implicit_return.rs index d29b508ba37..543948e39a7 100644 --- a/clippy_lints/src/implicit_return.rs +++ b/clippy_lints/src/implicit_return.rs @@ -7,6 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![warn(clippy::match_same_arms)] use crate::rustc::hir::{intravisit::FnKind, Body, ExprKind, FnDecl}; use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use crate::rustc::{declare_tool_lint, lint_array}; @@ -47,7 +48,8 @@ pub struct Pass; impl Pass { fn expr_match(cx: &LateContext<'_, '_>, expr: &rustc::hir::Expr) { match &expr.node { - ExprKind::Block(block, ..) => { + // loops could be using `break` instead of `return` + ExprKind::Block(block, ..) | ExprKind::Loop(block, ..) => { if let Some(expr) = &block.expr { Self::expr_match(cx, expr); } @@ -85,18 +87,6 @@ impl Pass { Self::expr_match(cx, &arm.body); } }, - // loops could be using `break` instead of `return` - ExprKind::Loop(block, ..) => { - if let Some(expr) = &block.expr { - Self::expr_match(cx, expr); - } - // only needed in the case of `break` with `;` at the end - else if let Some(stmt) = block.stmts.last() { - if let rustc::hir::StmtKind::Semi(expr, ..) = &stmt.node { - Self::expr_match(cx, expr); - } - } - }, // skip if it already has a return statement ExprKind::Ret(..) => (), // everything else is missing `return` From a4ec7be06fc3406bca536340a4073a9159d36e45 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Thu, 6 Dec 2018 13:23:42 +0100 Subject: [PATCH 3/3] Forgot to remove some debugging code ... --- clippy_lints/src/implicit_return.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/clippy_lints/src/implicit_return.rs b/clippy_lints/src/implicit_return.rs index 543948e39a7..07a849469fd 100644 --- a/clippy_lints/src/implicit_return.rs +++ b/clippy_lints/src/implicit_return.rs @@ -7,7 +7,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![warn(clippy::match_same_arms)] use crate::rustc::hir::{intravisit::FnKind, Body, ExprKind, FnDecl}; use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use crate::rustc::{declare_tool_lint, lint_array};