Skip to content

Commit 04f4471

Browse files
committed
Merge remote-tracking branch 'upstream/master' into rustup
2 parents 133e1d6 + d3e9db7 commit 04f4471

File tree

6 files changed

+51
-42
lines changed

6 files changed

+51
-42
lines changed

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ declare_clippy_lint! {
4040
/// assert_eq!(v.len(), 42);
4141
/// }
4242
/// ```
43-
///
43+
/// should be
4444
/// ```rust
45-
/// // should be
4645
/// fn foo(v: &[i32]) {
4746
/// assert_eq!(v.len(), 42);
4847
/// }
@@ -159,26 +158,19 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
159158
}
160159
}
161160

161+
//
162162
// * Exclude a type that is specifically bounded by `Borrow`.
163163
// * Exclude a type whose reference also fulfills its bound. (e.g., `std::convert::AsRef`,
164164
// `serde::Serialize`)
165165
let (implements_borrow_trait, all_borrowable_trait) = {
166-
let preds = preds
167-
.iter()
168-
.filter(|t| t.self_ty() == ty)
169-
.collect::<Vec<_>>();
166+
let preds = preds.iter().filter(|t| t.self_ty() == ty).collect::<Vec<_>>();
170167

171168
(
172169
preds.iter().any(|t| t.def_id() == borrow_trait),
173170
!preds.is_empty() && {
174171
let ty_empty_region = cx.tcx.mk_imm_ref(cx.tcx.lifetimes.re_root_empty, ty);
175172
preds.iter().all(|t| {
176-
let ty_params = t
177-
.trait_ref
178-
.substs
179-
.iter()
180-
.skip(1)
181-
.collect::<Vec<_>>();
173+
let ty_params = t.trait_ref.substs.iter().skip(1).collect::<Vec<_>>();
182174
implements_trait(cx, ty_empty_region, t.def_id(), &ty_params)
183175
})
184176
},

clippy_lints/src/suspicious_trait_impl.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,22 @@ impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
6464
| hir::BinOpKind::Gt => return,
6565
_ => {},
6666
}
67-
// Check if the binary expression is part of another bi/unary expression
68-
// or operator assignment as a child node
69-
let mut parent_expr = cx.tcx.hir().get_parent_node(expr.hir_id);
70-
while parent_expr != hir::CRATE_HIR_ID {
71-
if let hir::Node::Expr(e) = cx.tcx.hir().get(parent_expr) {
72-
match e.kind {
73-
hir::ExprKind::Binary(..)
74-
| hir::ExprKind::Unary(hir::UnOp::UnNot | hir::UnOp::UnNeg, _)
75-
| hir::ExprKind::AssignOp(..) => return,
76-
_ => {},
67+
68+
// Check for more than one binary operation in the implemented function
69+
// Linting when multiple operations are involved can result in false positives
70+
if_chain! {
71+
let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
72+
if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get(parent_fn);
73+
if let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind;
74+
let body = cx.tcx.hir().body(body_id);
75+
let mut visitor = BinaryExprVisitor { nb_binops: 0 };
76+
77+
then {
78+
walk_expr(&mut visitor, &body.value);
79+
if visitor.nb_binops > 1 {
80+
return;
7781
}
7882
}
79-
parent_expr = cx.tcx.hir().get_parent_node(parent_expr);
80-
}
81-
// as a parent node
82-
let mut visitor = BinaryExprVisitor { in_binary_expr: false };
83-
walk_expr(&mut visitor, expr);
84-
85-
if visitor.in_binary_expr {
86-
return;
8783
}
8884

8985
if let Some(impl_trait) = check_binop(
@@ -181,7 +177,7 @@ fn check_binop(
181177
}
182178

183179
struct BinaryExprVisitor {
184-
in_binary_expr: bool,
180+
nb_binops: u32,
185181
}
186182

187183
impl<'tcx> Visitor<'tcx> for BinaryExprVisitor {
@@ -191,12 +187,13 @@ impl<'tcx> Visitor<'tcx> for BinaryExprVisitor {
191187
match expr.kind {
192188
hir::ExprKind::Binary(..)
193189
| hir::ExprKind::Unary(hir::UnOp::UnNot | hir::UnOp::UnNeg, _)
194-
| hir::ExprKind::AssignOp(..) => self.in_binary_expr = true,
190+
| hir::ExprKind::AssignOp(..) => self.nb_binops += 1,
195191
_ => {},
196192
}
197193

198194
walk_expr(self, expr);
199195
}
196+
200197
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
201198
NestedVisitorMap::None
202199
}

clippy_lints/src/utils/ast_utils.rs

100755100644
File mode changed.

tests/compile-test.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ fn run_ui_toml(config: &mut compiletest::Config) {
147147
}
148148

149149
fn run_ui_cargo(config: &mut compiletest::Config) {
150-
if cargo::is_rustc_test_suite() {
151-
return;
152-
}
153150
fn run_tests(
154151
config: &compiletest::Config,
155152
filter: &Option<String>,

tests/ui/suspicious_arithmetic_impl.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,33 @@ fn main() {}
8888
fn do_nothing(x: u32) -> u32 {
8989
x
9090
}
91+
92+
struct MultipleBinops(u32);
93+
94+
impl Add for MultipleBinops {
95+
type Output = MultipleBinops;
96+
97+
// OK: multiple Binops in `add` impl
98+
fn add(self, other: Self) -> Self::Output {
99+
let mut result = self.0 + other.0;
100+
if result >= u32::max_value() {
101+
result -= u32::max_value();
102+
}
103+
MultipleBinops(result)
104+
}
105+
}
106+
107+
impl Mul for MultipleBinops {
108+
type Output = MultipleBinops;
109+
110+
// OK: multiple Binops in `mul` impl
111+
fn mul(self, other: Self) -> Self::Output {
112+
let mut result: u32 = 0;
113+
let size = std::cmp::max(self.0, other.0) as usize;
114+
let mut v = vec![0; size + 1];
115+
for i in 0..size + 1 {
116+
result *= i as u32;
117+
}
118+
MultipleBinops(result)
119+
}
120+
}

util/dev

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)