Skip to content

Commit e92c489

Browse files
committed
Auto merge of #4407 - mikerite:fix-breakage-20190818, r=flip1995
Fix breakage due to rust-lang/rust#61708 Includes commits from #4406 changelog: none
2 parents 49dff2c + b313d25 commit e92c489

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed
+20-25
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use if_chain::if_chain;
21
use rustc::hir::{Expr, ExprKind};
32
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
43
use rustc::{declare_lint_pass, declare_tool_lint};
5-
use syntax_pos::Span;
64

75
use crate::consts::{constant, Constant};
8-
use crate::utils::{in_macro_or_desugar, is_direct_expn_of, span_help_and_lint};
6+
use crate::utils::{in_macro_or_desugar, is_direct_expn_of, is_expn_of, span_help_and_lint};
97

108
declare_clippy_lint! {
119
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
@@ -33,42 +31,39 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
3331

3432
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
3533
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36-
let mut is_debug_assert = false;
37-
let debug_assert_not_in_macro_or_desugar = |span: Span| {
38-
is_debug_assert = true;
39-
// Check that `debug_assert!` itself is not inside a macro
40-
!in_macro_or_desugar(span)
41-
};
42-
if_chain! {
43-
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
44-
if !in_macro_or_desugar(assert_span)
45-
|| is_direct_expn_of(assert_span, "debug_assert")
46-
.map_or(false, debug_assert_not_in_macro_or_desugar);
47-
if let ExprKind::Unary(_, ref lit) = e.node;
48-
if let Some(bool_const) = constant(cx, cx.tables, lit);
49-
then {
50-
match bool_const.0 {
51-
Constant::Bool(true) => {
34+
let lint_assert_cb = |is_debug_assert: bool| {
35+
if let ExprKind::Unary(_, ref lit) = e.node {
36+
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit) {
37+
if is_true {
5238
span_help_and_lint(
5339
cx,
5440
ASSERTIONS_ON_CONSTANTS,
5541
e.span,
5642
"`assert!(true)` will be optimized out by the compiler",
57-
"remove it"
43+
"remove it",
5844
);
59-
},
60-
Constant::Bool(false) if !is_debug_assert => {
45+
} else if !is_debug_assert {
6146
span_help_and_lint(
6247
cx,
6348
ASSERTIONS_ON_CONSTANTS,
6449
e.span,
6550
"`assert!(false)` should probably be replaced",
66-
"use `panic!()` or `unreachable!()`"
51+
"use `panic!()` or `unreachable!()`",
6752
);
68-
},
69-
_ => (),
53+
}
7054
}
7155
}
56+
};
57+
if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
58+
if in_macro_or_desugar(debug_assert_span) {
59+
return;
60+
}
61+
lint_assert_cb(true);
62+
} else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
63+
if in_macro_or_desugar(assert_span) {
64+
return;
65+
}
66+
lint_assert_cb(false);
7267
}
7368
}
7469
}

clippy_lints/src/copies.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,14 @@ fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap<LocalI
298298
bindings_impl(cx, as_pat, map);
299299
}
300300
},
301-
PatKind::Struct(_, ref fields, _) => {
301+
PatKind::Or(ref fields) | PatKind::Tuple(ref fields, _) => {
302302
for pat in fields {
303-
bindings_impl(cx, &pat.pat, map);
303+
bindings_impl(cx, pat, map);
304304
}
305305
},
306-
PatKind::Tuple(ref fields, _) => {
306+
PatKind::Struct(_, ref fields, _) => {
307307
for pat in fields {
308-
bindings_impl(cx, pat, map);
308+
bindings_impl(cx, &pat.pat, map);
309309
}
310310
},
311311
PatKind::Slice(ref lhs, ref mid, ref rhs) => {

clippy_lints/src/utils/author.rs

+6
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,12 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
549549
println!(" if {}.len() == {};", fields_pat, fields.len());
550550
println!(" // unimplemented: field checks");
551551
},
552+
PatKind::Or(ref fields) => {
553+
let fields_pat = self.next("fields");
554+
println!("Or(ref {}) = {};", fields_pat, current);
555+
println!(" if {}.len() == {};", fields_pat, fields.len());
556+
println!(" // unimplemented: field checks");
557+
},
552558
PatKind::TupleStruct(ref path, ref fields, skip_pos) => {
553559
let path_pat = self.next("path");
554560
let fields_pat = self.next("fields");

clippy_lints/src/utils/inspector.rs

+6
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) {
410410
print_pat(cx, inner, indent + 1);
411411
}
412412
},
413+
hir::PatKind::Or(ref fields) => {
414+
println!("{}Or", ind);
415+
for field in fields {
416+
print_pat(cx, field, indent + 1);
417+
}
418+
},
413419
hir::PatKind::Struct(ref path, ref fields, ignore) => {
414420
println!("{}Struct", ind);
415421
println!(

clippy_lints/src/utils/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ pub fn is_refutable(cx: &LateContext<'_, '_>, pat: &Pat) -> bool {
791791
PatKind::Box(ref pat) | PatKind::Ref(ref pat, _) => is_refutable(cx, pat),
792792
PatKind::Lit(..) | PatKind::Range(..) => true,
793793
PatKind::Path(ref qpath) => is_enum_variant(cx, qpath, pat.hir_id),
794-
PatKind::Tuple(ref pats, _) => are_refutable(cx, pats.iter().map(|pat| &**pat)),
794+
PatKind::Or(ref pats) | PatKind::Tuple(ref pats, _) => are_refutable(cx, pats.iter().map(|pat| &**pat)),
795795
PatKind::Struct(ref qpath, ref fields, _) => {
796796
if is_enum_variant(cx, qpath, pat.hir_id) {
797797
true

0 commit comments

Comments
 (0)