From 0b0aeaca43bc457db2be70114fa7c6ab7925fd78 Mon Sep 17 00:00:00 2001 From: wangxiangqing Date: Sun, 6 Oct 2019 12:03:53 +0800 Subject: [PATCH 1/3] Suggest dereferencing boolean reference when used in 'if' or 'while' Change-Id: I0c5c4d767be2647e6f017ae7bf83558c56dbca97 --- src/librustc_typeck/check/demand.rs | 6 +++++- src/librustc_typeck/check/expr.rs | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 2ea0afb179356..78bd4508e21a4 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -349,7 +349,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // If the span is from a macro, then it's hard to extract the text // and make a good suggestion, so don't bother. - let is_macro = sp.from_expansion(); + let is_desugaring = match sp.desugaring_kind() { + Some(k) => sp.is_desugaring(k), + None => false + }; + let is_macro = sp.from_expansion() && !is_desugaring; match (&expr.kind, &expected.kind, &checked_ty.kind) { (_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.kind, &check.kind) { diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 7a6fe9560fbff..aa26c74967a1e 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -87,6 +87,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) { + self.suggest_ref_or_into(&mut err, expr, expected_ty, ty); + let expr = match &expr.kind { ExprKind::DropTemps(expr) => expr, _ => expr, From 8a164acf5879aec3389180f7102ea32dce5eb07a Mon Sep 17 00:00:00 2001 From: wangxiangqing Date: Sun, 6 Oct 2019 12:03:53 +0800 Subject: [PATCH 2/3] Suggest dereferencing boolean reference when used in 'if' or 'while' Change-Id: I0c5c4d767be2647e6f017ae7bf83558c56dbca97 --- src/test/ui/if/if-no-match-bindings.stderr | 44 +++++++++++++---- .../disallowed-positions.stderr | 48 ++++++++++++++++++- 2 files changed, 80 insertions(+), 12 deletions(-) diff --git a/src/test/ui/if/if-no-match-bindings.stderr b/src/test/ui/if/if-no-match-bindings.stderr index cbf52476ae37f..694b59897c5a9 100644 --- a/src/test/ui/if/if-no-match-bindings.stderr +++ b/src/test/ui/if/if-no-match-bindings.stderr @@ -2,7 +2,10 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:18:8 | LL | if b_ref() {} - | ^^^^^^^ expected bool, found &bool + | ^^^^^^^ + | | + | expected bool, found &bool + | help: consider dereferencing the borrow: `*b_ref()` | = note: expected type `bool` found type `&bool` @@ -11,7 +14,10 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:19:8 | LL | if b_mut_ref() {} - | ^^^^^^^^^^^ expected bool, found &mut bool + | ^^^^^^^^^^^ + | | + | expected bool, found &mut bool + | help: consider dereferencing the borrow: `*b_mut_ref()` | = note: expected type `bool` found type `&mut bool` @@ -20,7 +26,10 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:20:8 | LL | if &true {} - | ^^^^^ expected bool, found &bool + | ^^^^^ + | | + | expected bool, found &bool + | help: consider dereferencing the borrow: `*&true` | = note: expected type `bool` found type `&bool` @@ -29,7 +38,10 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:21:8 | LL | if &mut true {} - | ^^^^^^^^^ expected bool, found &mut bool + | ^^^^^^^^^ + | | + | expected bool, found &mut bool + | help: consider dereferencing the borrow: `*&mut true` | = note: expected type `bool` found type `&mut bool` @@ -38,7 +50,10 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:24:11 | LL | while b_ref() {} - | ^^^^^^^ expected bool, found &bool + | ^^^^^^^ + | | + | expected bool, found &bool + | help: consider dereferencing the borrow: `*b_ref()` | = note: expected type `bool` found type `&bool` @@ -47,7 +62,10 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:25:11 | LL | while b_mut_ref() {} - | ^^^^^^^^^^^ expected bool, found &mut bool + | ^^^^^^^^^^^ + | | + | expected bool, found &mut bool + | help: consider dereferencing the borrow: `*b_mut_ref()` | = note: expected type `bool` found type `&mut bool` @@ -55,8 +73,11 @@ LL | while b_mut_ref() {} error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:26:11 | -LL | while &true {} - | ^^^^^ expected bool, found &bool +26 | while &true {} + | ^^^^^ + | | + | expected bool, found &bool + | help: consider dereferencing the borrow: `*&true` | = note: expected type `bool` found type `&bool` @@ -64,8 +85,11 @@ LL | while &true {} error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:27:11 | -LL | while &mut true {} - | ^^^^^^^^^ expected bool, found &mut bool +27 | while &mut true {} + | ^^^^^^^^^ + | | + | expected bool, found &mut bool + | help: consider dereferencing the borrow: `*&mut true` | = note: expected type `bool` found type `&mut bool` diff --git a/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr b/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr index 4edc00efc7e72..ab1b405e5c21f 100644 --- a/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr +++ b/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr @@ -513,11 +513,52 @@ warning: the feature `let_chains` is incomplete and may cause the compiler to cr LL | #![feature(let_chains)] // Avoid inflating `.stderr` with overzealous gates in this test. | ^^^^^^^^^^ +warning: unnecessary parentheses around `if` condition + --> $DIR/disallowed-positions.rs:51:8 + | +LL | if (true || let 0 = 0) {} + | ^^^^^^^^^^^^^^^^^^^ help: remove these parentheses + | + = note: `#[warn(unused_parens)]` on by default + +warning: unnecessary parentheses around `while` condition + --> $DIR/disallowed-positions.rs:115:11 + | +LL | while (true || let 0 = 0) {} + | ^^^^^^^^^^^^^^^^^^^ help: remove these parentheses + +wrning: unnecessary parentheses around `let` head expression + --> $DIR/disallowed-positions.rs:160:41 + | +LL | if let Range { start: _, end: _ } = (true..true || false) { } + | ^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses + +warning: unnecessary parentheses around `let` head expression + --> $DIR/disallowed-positions.rs:162:41 + | +LL | if let Range { start: _, end: _ } = (true..true && false) { } + | ^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses + +warning: unnecessary parentheses around `let` head expression + --> $DIR/disallowed-positions.rs:164:44 + | +LL | while let Range { start: _, end: _ } = (true..true || false) { } + | ^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses + +warning: unnecessary parentheses around `let` head expression + --> $DIR/disallowed-positions.rs:166:44 + | +LL | while let Range { start: _, end: _ } = (true..true && false) { } + | ^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses + error[E0308]: mismatched types --> $DIR/disallowed-positions.rs:32:8 | LL | if &let 0 = 0 {} - | ^^^^^^^^^^ expected bool, found &bool + | ^^^^^^^^^^ + | | + | expected bool, found &bool + | help: consider dereferencing the borrow: `*&let 0 = 0` | = note: expected type `bool` found type `&bool` @@ -702,7 +743,10 @@ error[E0308]: mismatched types --> $DIR/disallowed-positions.rs:96:11 | LL | while &let 0 = 0 {} - | ^^^^^^^^^^ expected bool, found &bool + | ^^^^^^^^^^ + | | + | expected bool, found &bool + | help: consider dereferencing the borrow: `*&let 0 = 0` | = note: expected type `bool` found type `&bool` From bbb69d14552b7258ba0b1d56d6558bd32b647b9a Mon Sep 17 00:00:00 2001 From: wangxiangqing Date: Sun, 6 Oct 2019 12:03:53 +0800 Subject: [PATCH 3/3] Suggest dereferencing boolean reference when used in 'if' or 'while' Change-Id: I0c5c4d767be2647e6f017ae7bf83558c56dbca97 --- src/test/ui/if/if-no-match-bindings.stderr | 4 +- .../disallowed-positions.stderr | 38 ------------------- 2 files changed, 2 insertions(+), 40 deletions(-) diff --git a/src/test/ui/if/if-no-match-bindings.stderr b/src/test/ui/if/if-no-match-bindings.stderr index 694b59897c5a9..53b7aafc430a2 100644 --- a/src/test/ui/if/if-no-match-bindings.stderr +++ b/src/test/ui/if/if-no-match-bindings.stderr @@ -73,7 +73,7 @@ LL | while b_mut_ref() {} error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:26:11 | -26 | while &true {} +LL | while &true {} | ^^^^^ | | | expected bool, found &bool @@ -85,7 +85,7 @@ error[E0308]: mismatched types error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:27:11 | -27 | while &mut true {} +LL | while &mut true {} | ^^^^^^^^^ | | | expected bool, found &mut bool diff --git a/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr b/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr index ab1b405e5c21f..619f9c85b24db 100644 --- a/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr +++ b/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr @@ -513,44 +513,6 @@ warning: the feature `let_chains` is incomplete and may cause the compiler to cr LL | #![feature(let_chains)] // Avoid inflating `.stderr` with overzealous gates in this test. | ^^^^^^^^^^ -warning: unnecessary parentheses around `if` condition - --> $DIR/disallowed-positions.rs:51:8 - | -LL | if (true || let 0 = 0) {} - | ^^^^^^^^^^^^^^^^^^^ help: remove these parentheses - | - = note: `#[warn(unused_parens)]` on by default - -warning: unnecessary parentheses around `while` condition - --> $DIR/disallowed-positions.rs:115:11 - | -LL | while (true || let 0 = 0) {} - | ^^^^^^^^^^^^^^^^^^^ help: remove these parentheses - -wrning: unnecessary parentheses around `let` head expression - --> $DIR/disallowed-positions.rs:160:41 - | -LL | if let Range { start: _, end: _ } = (true..true || false) { } - | ^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses - -warning: unnecessary parentheses around `let` head expression - --> $DIR/disallowed-positions.rs:162:41 - | -LL | if let Range { start: _, end: _ } = (true..true && false) { } - | ^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses - -warning: unnecessary parentheses around `let` head expression - --> $DIR/disallowed-positions.rs:164:44 - | -LL | while let Range { start: _, end: _ } = (true..true || false) { } - | ^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses - -warning: unnecessary parentheses around `let` head expression - --> $DIR/disallowed-positions.rs:166:44 - | -LL | while let Range { start: _, end: _ } = (true..true && false) { } - | ^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses - error[E0308]: mismatched types --> $DIR/disallowed-positions.rs:32:8 |