mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-22 03:44:24 +00:00
actually fix it
This commit is contained in:
parent
f4b02aa374
commit
5825b9e3e2
@ -1,7 +1,7 @@
|
|||||||
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
|
||||||
use clippy_utils::{get_parent_expr, path_to_local, path_to_local_id};
|
use clippy_utils::{get_parent_expr, path_to_local, path_to_local_id};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
|
use rustc_hir::intravisit::{walk_expr, Visitor};
|
||||||
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Guard, HirId, Local, Node, Stmt, StmtKind};
|
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Guard, HirId, Local, Node, Stmt, StmtKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
@ -114,7 +114,7 @@ struct DivergenceVisitor<'a, 'tcx> {
|
|||||||
impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
|
impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
|
||||||
fn maybe_walk_expr(&mut self, e: &'tcx Expr<'_>) {
|
fn maybe_walk_expr(&mut self, e: &'tcx Expr<'_>) {
|
||||||
match e.kind {
|
match e.kind {
|
||||||
ExprKind::Closure { .. } => {},
|
ExprKind::Closure(..) | ExprKind::If(..) | ExprKind::Loop(..) => {},
|
||||||
ExprKind::Match(e, arms, _) => {
|
ExprKind::Match(e, arms, _) => {
|
||||||
self.visit_expr(e);
|
self.visit_expr(e);
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
@ -128,6 +128,7 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
|
|||||||
_ => walk_expr(self, e),
|
_ => walk_expr(self, e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn report_diverging_sub_expr(&mut self, e: &Expr<'_>) {
|
fn report_diverging_sub_expr(&mut self, e: &Expr<'_>) {
|
||||||
span_lint(self.cx, DIVERGING_SUB_EXPRESSION, e.span, "sub-expression diverges");
|
span_lint(self.cx, DIVERGING_SUB_EXPRESSION, e.span, "sub-expression diverges");
|
||||||
}
|
}
|
||||||
@ -136,6 +137,15 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
|
|||||||
impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
|
impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
|
||||||
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
|
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
|
||||||
match e.kind {
|
match e.kind {
|
||||||
|
// fix #10776
|
||||||
|
ExprKind::Block(block, ..) => {
|
||||||
|
if let Some(stmt) = block.stmts.first() && block.stmts.len() == 1 {
|
||||||
|
match stmt.kind {
|
||||||
|
StmtKind::Expr(e) | StmtKind::Semi(e) => self.visit_expr(e),
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
ExprKind::Continue(_) | ExprKind::Break(_, _) | ExprKind::Ret(_) => self.report_diverging_sub_expr(e),
|
ExprKind::Continue(_) | ExprKind::Break(_, _) | ExprKind::Ret(_) => self.report_diverging_sub_expr(e),
|
||||||
ExprKind::Call(func, _) => {
|
ExprKind::Call(func, _) => {
|
||||||
let typ = self.cx.typeck_results().expr_ty(func);
|
let typ = self.cx.typeck_results().expr_ty(func);
|
||||||
@ -155,7 +165,6 @@ impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
|
|||||||
self.report_diverging_sub_expr(e);
|
self.report_diverging_sub_expr(e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ExprKind::Block(block, ..) => walk_block(self, block),
|
|
||||||
_ => {
|
_ => {
|
||||||
// do not lint expressions referencing objects of type `!`, as that required a
|
// do not lint expressions referencing objects of type `!`, as that required a
|
||||||
// diverging expression
|
// diverging expression
|
||||||
@ -164,6 +173,9 @@ impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
self.maybe_walk_expr(e);
|
self.maybe_walk_expr(e);
|
||||||
}
|
}
|
||||||
|
fn visit_block(&mut self, _: &'tcx Block<'_>) {
|
||||||
|
// don't continue over blocks, LateLintPass already does that
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Walks up the AST from the given write expression (`vis.write_expr`) looking
|
/// Walks up the AST from the given write expression (`vis.write_expr`) looking
|
||||||
|
@ -22,6 +22,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code, unused_variables)]
|
#[allow(dead_code, unused_variables)]
|
||||||
|
#[rustfmt::skip]
|
||||||
fn foobar() {
|
fn foobar() {
|
||||||
loop {
|
loop {
|
||||||
let x = match 5 {
|
let x = match 5 {
|
||||||
|
@ -126,6 +126,14 @@ LL | | }
|
|||||||
LL | | }
|
LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
||||||
|
error: sub-expression diverges
|
||||||
|
--> $DIR/never_loop.rs:247:17
|
||||||
|
|
|
||||||
|
LL | break 'a;
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: `-D clippy::diverging-sub-expression` implied by `-D warnings`
|
||||||
|
|
||||||
error: this loop never actually loops
|
error: this loop never actually loops
|
||||||
--> $DIR/never_loop.rs:278:13
|
--> $DIR/never_loop.rs:278:13
|
||||||
|
|
|
|
||||||
@ -139,5 +147,5 @@ help: if you need the first element of the iterator, try writing
|
|||||||
LL | if let Some(_) = (0..20).next() {
|
LL | if let Some(_) = (0..20).next() {
|
||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
error: aborting due to 12 previous errors
|
error: aborting due to 13 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user