Skip to content

Commit 9c7fce0

Browse files
committed
submodules: update clippy from 1838bfe to 280069d
Changes: ```` Rustfmt all the things Don't make decisions on values that don't represent the decision Rustup Actually check for constants. formatting fix Update clippy_lints/src/needless_bool.rs formatting fix needless bool lint suggestion is wrapped in brackets if it is an "else" clause of an "if-else" statement Remove negative integer literal checks. Fix `implicit_return` false positives. ````
1 parent 96905b8 commit 9c7fce0

File tree

10 files changed

+124
-25
lines changed

10 files changed

+124
-25
lines changed

clippy_lints/src/arithmetic.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::consts::constant_simple;
12
use crate::utils::span_lint;
23
use rustc::hir;
34
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -94,8 +95,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
9495
hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
9596
let ty = cx.tables.expr_ty(arg);
9697
if ty.is_integral() {
97-
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
98-
self.expr_span = Some(expr.span);
98+
if constant_simple(cx, cx.tables, expr).is_none() {
99+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
100+
self.expr_span = Some(expr.span);
101+
}
99102
} else if ty.is_floating_point() {
100103
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
101104
self.expr_span = Some(expr.span);
@@ -125,7 +128,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
125128
}
126129
self.const_span = Some(body_span);
127130
},
128-
hir::BodyOwnerKind::Fn => (),
131+
hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
129132
}
130133
}
131134

clippy_lints/src/implicit_return.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::utils::{in_macro, snippet_opt, span_lint_and_then};
2-
use rustc::hir::{intravisit::FnKind, Body, ExprKind, FnDecl};
1+
use crate::utils::{in_macro, is_expn_of, snippet_opt, span_lint_and_then};
2+
use rustc::hir::{intravisit::FnKind, Body, ExprKind, FnDecl, MatchSource};
33
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
44
use rustc::{declare_tool_lint, lint_array};
55
use rustc_errors::Applicability;
@@ -81,15 +81,31 @@ impl Pass {
8181
Self::expr_match(cx, else_expr);
8282
}
8383
},
84-
ExprKind::Match(_, arms, ..) => {
85-
for arm in arms {
86-
Self::expr_match(cx, &arm.body);
84+
ExprKind::Match(.., arms, source) => {
85+
let check_all_arms = match source {
86+
MatchSource::IfLetDesugar {
87+
contains_else_clause: has_else,
88+
} => *has_else,
89+
_ => true,
90+
};
91+
92+
if check_all_arms {
93+
for arm in arms {
94+
Self::expr_match(cx, &arm.body);
95+
}
96+
} else {
97+
Self::expr_match(cx, &arms.first().expect("if let doesn't have a single arm").body);
8798
}
8899
},
89100
// skip if it already has a return statement
90101
ExprKind::Ret(..) => (),
91102
// everything else is missing `return`
92-
_ => Self::lint(cx, expr.span, expr.span, "add `return` as shown"),
103+
_ => {
104+
// make sure it's not just an unreachable expression
105+
if is_expn_of(expr.span, "unreachable").is_none() {
106+
Self::lint(cx, expr.span, expr.span, "add `return` as shown")
107+
}
108+
},
93109
}
94110
}
95111
}

clippy_lints/src/needless_bool.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
7272
let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
7373
let snip = if not { !snip } else { snip };
7474

75-
let hint = if ret {
75+
let mut hint = if ret {
7676
format!("return {}", snip)
7777
} else {
7878
snip.to_string()
7979
};
8080

81+
if parent_node_is_if_expr(&e, &cx) {
82+
hint = format!("{{ {} }}", hint);
83+
}
84+
8185
span_lint_and_sugg(
8286
cx,
8387
NEEDLESS_BOOL,
@@ -119,6 +123,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
119123
}
120124
}
121125

126+
fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) -> bool {
127+
let parent_id = cx.tcx.hir().get_parent_node(expr.id);
128+
let parent_node = cx.tcx.hir().get(parent_id);
129+
130+
if let rustc::hir::Node::Expr(e) = parent_node {
131+
if let ExprKind::If(_, _, _) = e.node {
132+
return true;
133+
}
134+
}
135+
136+
false
137+
}
138+
122139
#[derive(Copy, Clone)]
123140
pub struct BoolComparison;
124141

clippy_lints/src/utils/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,25 @@ pub fn differing_macro_contexts(lhs: Span, rhs: Span) -> bool {
6464
/// ```
6565
pub fn in_constant(cx: &LateContext<'_, '_>, id: NodeId) -> bool {
6666
let parent_id = cx.tcx.hir().get_parent(id);
67-
match cx.tcx.hir().body_owner_kind(parent_id) {
68-
hir::BodyOwnerKind::Fn => false,
69-
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(..) => true,
67+
match cx.tcx.hir().get(parent_id) {
68+
Node::Item(&Item {
69+
node: ItemKind::Const(..),
70+
..
71+
})
72+
| Node::TraitItem(&TraitItem {
73+
node: TraitItemKind::Const(..),
74+
..
75+
})
76+
| Node::ImplItem(&ImplItem {
77+
node: ImplItemKind::Const(..),
78+
..
79+
})
80+
| Node::AnonConst(_)
81+
| Node::Item(&Item {
82+
node: ItemKind::Static(..),
83+
..
84+
}) => true,
85+
_ => false,
7086
}
7187
}
7288

tests/ui/arithmetic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ fn main() {
1717
i - 2 + 2 - i;
1818
-i;
1919

20+
// no error, overflows are checked by `overflowing_literals`
21+
-1;
22+
-(-1);
23+
2024
i & 1; // no wrapping
2125
i | 1;
2226
i ^ 1;

tests/ui/arithmetic.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,39 @@ LL | -i;
3232
| ^^
3333

3434
error: floating-point arithmetic detected
35-
--> $DIR/arithmetic.rs:28:5
35+
--> $DIR/arithmetic.rs:32:5
3636
|
3737
LL | f * 2.0;
3838
| ^^^^^^^
3939
|
4040
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
4141

4242
error: floating-point arithmetic detected
43-
--> $DIR/arithmetic.rs:30:5
43+
--> $DIR/arithmetic.rs:34:5
4444
|
4545
LL | 1.0 + f;
4646
| ^^^^^^^
4747

4848
error: floating-point arithmetic detected
49-
--> $DIR/arithmetic.rs:31:5
49+
--> $DIR/arithmetic.rs:35:5
5050
|
5151
LL | f * 2.0;
5252
| ^^^^^^^
5353

5454
error: floating-point arithmetic detected
55-
--> $DIR/arithmetic.rs:32:5
55+
--> $DIR/arithmetic.rs:36:5
5656
|
5757
LL | f / 2.0;
5858
| ^^^^^^^
5959

6060
error: floating-point arithmetic detected
61-
--> $DIR/arithmetic.rs:33:5
61+
--> $DIR/arithmetic.rs:37:5
6262
|
6363
LL | f - 2.0 * 4.2;
6464
| ^^^^^^^^^^^^^
6565

6666
error: floating-point arithmetic detected
67-
--> $DIR/arithmetic.rs:34:5
67+
--> $DIR/arithmetic.rs:38:5
6868
|
6969
LL | -f;
7070
| ^^

tests/ui/implicit_return.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ fn test_match(x: bool) -> bool {
2626
}
2727
}
2828

29+
#[allow(clippy::match_bool, clippy::needless_return)]
30+
fn test_match_with_unreachable(x: bool) -> bool {
31+
match x {
32+
true => return false,
33+
false => unreachable!(),
34+
}
35+
}
36+
2937
#[allow(clippy::never_loop)]
3038
fn test_loop() -> bool {
3139
loop {
@@ -53,6 +61,15 @@ fn test_loop_with_nests() -> bool {
5361
}
5462
}
5563

64+
#[allow(clippy::redundant_pattern_matching)]
65+
fn test_loop_with_if_let() -> bool {
66+
loop {
67+
if let Some(x) = Some(true) {
68+
return x;
69+
}
70+
}
71+
}
72+
5673
fn test_closure() {
5774
#[rustfmt::skip]
5875
let _ = || { true };
@@ -63,8 +80,10 @@ fn main() {
6380
let _ = test_end_of_fn();
6481
let _ = test_if_block();
6582
let _ = test_match(true);
83+
let _ = test_match_with_unreachable(true);
6684
let _ = test_loop();
6785
let _ = test_loop_with_block();
6886
let _ = test_loop_with_nests();
87+
let _ = test_loop_with_if_let();
6988
test_closure();
7089
}

tests/ui/implicit_return.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@ LL | false => { true },
3131
| ^^^^ help: add `return` as shown: `return true`
3232

3333
error: missing return statement
34-
--> $DIR/implicit_return.rs:32:9
34+
--> $DIR/implicit_return.rs:40:9
3535
|
3636
LL | break true;
3737
| ^^^^^^^^^^ help: change `break` to `return` as shown: `return true`
3838

3939
error: missing return statement
40-
--> $DIR/implicit_return.rs:40:13
40+
--> $DIR/implicit_return.rs:48:13
4141
|
4242
LL | break true;
4343
| ^^^^^^^^^^ help: change `break` to `return` as shown: `return true`
4444

4545
error: missing return statement
46-
--> $DIR/implicit_return.rs:49:13
46+
--> $DIR/implicit_return.rs:57:13
4747
|
4848
LL | break true;
4949
| ^^^^^^^^^^ help: change `break` to `return` as shown: `return true`
5050

5151
error: missing return statement
52-
--> $DIR/implicit_return.rs:58:18
52+
--> $DIR/implicit_return.rs:75:18
5353
|
5454
LL | let _ = || { true };
5555
| ^^^^ help: add `return` as shown: `return true`
5656

5757
error: missing return statement
58-
--> $DIR/implicit_return.rs:59:16
58+
--> $DIR/implicit_return.rs:76:16
5959
|
6060
LL | let _ = || true;
6161
| ^^^^ help: add `return` as shown: `return true`

tests/ui/needless_bool.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,16 @@ fn needless_bool3(x: bool) {
141141
if x == true {};
142142
if x == false {};
143143
}
144+
145+
fn needless_bool_in_the_suggestion_wraps_the_predicate_of_if_else_statement_in_brackets() {
146+
let b = false;
147+
let returns_bool = || false;
148+
149+
let x = if b {
150+
true
151+
} else if returns_bool() {
152+
false
153+
} else {
154+
true
155+
};
156+
}

tests/ui/needless_bool.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,16 @@ error: equality checks against false can be replaced by a negation
136136
LL | if x == false {};
137137
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
138138

139-
error: aborting due to 15 previous errors
139+
error: this if-then-else expression returns a bool literal
140+
--> $DIR/needless_bool.rs:151:12
141+
|
142+
LL | } else if returns_bool() {
143+
| ____________^
144+
LL | | false
145+
LL | | } else {
146+
LL | | true
147+
LL | | };
148+
| |_____^ help: you can reduce it to: `{ !returns_bool() }`
149+
150+
error: aborting due to 16 previous errors
140151

0 commit comments

Comments
 (0)