Skip to content

Commit 6ffe725

Browse files
committed
Auto merge of rust-lang#5520 - matthiaskrgr:rustup_44, r=flip1995,phansch
rustup rust-lang#71215 There's currently an crash in `ui/new_without_default.rs` that I need to figure out how to avoid. changelog: none
2 parents 02c9435 + dda1c8d commit 6ffe725

16 files changed

+212
-50
lines changed

clippy_lints/src/cognitive_complexity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CognitiveComplexity {
123123
hir_id: HirId,
124124
) {
125125
let def_id = cx.tcx.hir().local_def_id(hir_id);
126-
if !cx.tcx.has_attr(def_id, sym!(test)) {
126+
if !cx.tcx.has_attr(def_id.to_def_id(), sym!(test)) {
127127
self.check(cx, kind, decl, body, span);
128128
}
129129
}

clippy_lints/src/derive.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,20 @@ fn check_hash_peq<'a, 'tcx>(
160160
};
161161

162162
span_lint_and_then(
163-
cx, DERIVE_HASH_XOR_EQ, span,
163+
cx,
164+
DERIVE_HASH_XOR_EQ,
165+
span,
164166
mess,
165167
|diag| {
166-
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(impl_id) {
167-
diag.span_note(
168-
cx.tcx.hir().span(hir_id),
169-
"`PartialEq` implemented here"
170-
);
168+
if let Some(local_def_id) = impl_id.as_local() {
169+
let hir_id = cx.tcx.hir().as_local_hir_id(local_def_id);
170+
diag.span_note(
171+
cx.tcx.hir().span(hir_id),
172+
"`PartialEq` implemented here"
173+
);
174+
}
171175
}
172-
});
176+
);
173177
}
174178
});
175179
}
@@ -225,7 +229,7 @@ fn check_unsafe_derive_deserialize<'a, 'tcx>(
225229
ty: Ty<'tcx>,
226230
) {
227231
fn item_from_def_id<'tcx>(cx: &LateContext<'_, 'tcx>, def_id: DefId) -> &'tcx Item<'tcx> {
228-
let hir_id = cx.tcx.hir().as_local_hir_id(def_id).unwrap();
232+
let hir_id = cx.tcx.hir().as_local_hir_id(def_id.expect_local());
229233
cx.tcx.hir().expect_item(hir_id)
230234
}
231235

clippy_lints/src/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DocMarkdown {
155155
let headers = check_attrs(cx, &self.valid_idents, &item.attrs);
156156
match item.kind {
157157
hir::ItemKind::Fn(ref sig, _, body_id) => {
158-
if !(is_entrypoint_fn(cx, cx.tcx.hir().local_def_id(item.hir_id))
158+
if !(is_entrypoint_fn(cx, cx.tcx.hir().local_def_id(item.hir_id).to_def_id())
159159
|| in_external_macro(cx.tcx.sess, item.span))
160160
{
161161
lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers, Some(body_id));

clippy_lints/src/escape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
7777

7878
let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
7979
cx.tcx.infer_ctxt().enter(|infcx| {
80-
ExprUseVisitor::new(&mut v, &infcx, fn_def_id, cx.param_env, cx.tables).consume_body(body);
80+
ExprUseVisitor::new(&mut v, &infcx, fn_def_id.to_def_id(), cx.param_env, cx.tables).consume_body(body);
8181
});
8282

8383
for node in v.set {

clippy_lints/src/exit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
3737
// If the next item up is a function we check if it is an entry point
3838
// and only then emit a linter warning
3939
let def_id = cx.tcx.hir().local_def_id(parent);
40-
if !is_entrypoint_fn(cx, def_id) {
40+
if !is_entrypoint_fn(cx, def_id.to_def_id()) {
4141
span_lint(cx, EXIT, e.span, "usage of `process::exit`");
4242
}
4343
}

clippy_lints/src/len_zero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn check_trait_items(cx: &LateContext<'_, '_>, visited_trait: &Item<'_>, trait_i
143143
if cx.access_levels.is_exported(visited_trait.hir_id) && trait_items.iter().any(|i| is_named_self(cx, i, "len")) {
144144
let mut current_and_super_traits = FxHashSet::default();
145145
let visited_trait_def_id = cx.tcx.hir().local_def_id(visited_trait.hir_id);
146-
fill_trait_set(visited_trait_def_id, &mut current_and_super_traits, cx);
146+
fill_trait_set(visited_trait_def_id.to_def_id(), &mut current_and_super_traits, cx);
147147

148148
let is_empty_method_found = current_and_super_traits
149149
.iter()

clippy_lints/src/loops.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -576,20 +576,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
576576
&& !is_iterator_used_after_while_let(cx, iter_expr)
577577
&& !is_nested(cx, expr, &method_args[0]))
578578
{
579-
let iterator = snippet(cx, method_args[0].span, "_");
579+
let mut applicability = Applicability::MachineApplicable;
580+
let iterator = snippet_with_applicability(cx, method_args[0].span, "_", &mut applicability);
580581
let loop_var = if pat_args.is_empty() {
581582
"_".to_string()
582583
} else {
583-
snippet(cx, pat_args[0].span, "_").into_owned()
584+
snippet_with_applicability(cx, pat_args[0].span, "_", &mut applicability).into_owned()
584585
};
585586
span_lint_and_sugg(
586587
cx,
587588
WHILE_LET_ON_ITERATOR,
588-
expr.span,
589+
expr.span.with_hi(match_expr.span.hi()),
589590
"this loop could be written as a `for` loop",
590591
"try",
591-
format!("for {} in {} {{ .. }}", loop_var, iterator),
592-
Applicability::HasPlaceholders,
592+
format!("for {} in {}", loop_var, iterator),
593+
applicability,
593594
);
594595
}
595596
}

clippy_lints/src/missing_const_for_fn.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
8383
) {
8484
let def_id = cx.tcx.hir().local_def_id(hir_id);
8585

86-
if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id) {
86+
if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) {
8787
return;
8888
}
8989

9090
// Building MIR for `fn`s with unsatisfiable preds results in ICE.
91-
if fn_has_unsatisfiable_preds(cx, def_id) {
91+
if fn_has_unsatisfiable_preds(cx, def_id.to_def_id()) {
9292
return;
9393
}
9494

@@ -118,8 +118,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
118118

119119
let mir = cx.tcx.optimized_mir(def_id);
120120

121-
if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) {
122-
if rustc_mir::const_eval::is_min_const_fn(cx.tcx, def_id) {
121+
if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id.to_def_id(), &mir) {
122+
if rustc_mir::const_eval::is_min_const_fn(cx.tcx, def_id.to_def_id()) {
123123
cx.tcx.sess.span_err(span, &err);
124124
}
125125
} else {

clippy_lints/src/missing_inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
152152
};
153153

154154
if let Some(trait_def_id) = trait_def_id {
155-
if cx.tcx.hir().as_local_hir_id(trait_def_id).is_some() && !cx.access_levels.is_exported(impl_item.hir_id) {
155+
if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.hir_id) {
156156
// If a trait is being implemented for an item, and the
157157
// trait is not exported, we don't need #[inline]
158158
return;

clippy_lints/src/needless_pass_by_value.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
135135
} = {
136136
let mut ctx = MovedVariablesCtxt::default();
137137
cx.tcx.infer_ctxt().enter(|infcx| {
138-
euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id, cx.param_env, cx.tables).consume_body(body);
138+
euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id.to_def_id(), cx.param_env, cx.tables)
139+
.consume_body(body);
139140
});
140141
ctx
141142
};

clippy_lints/src/new_without_default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
136136
let mut impls = HirIdSet::default();
137137
cx.tcx.for_each_impl(default_trait_id, |d| {
138138
if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() {
139-
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(ty_def.did) {
140-
impls.insert(hir_id);
139+
if let Some(local_def_id) = ty_def.did.as_local() {
140+
impls.insert(cx.tcx.hir().as_local_hir_id(local_def_id));
141141
}
142142
}
143143
});

clippy_lints/src/utils/inspector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ fn print_item(cx: &LateContext<'_, '_>, item: &hir::Item<'_>) {
378378
},
379379
hir::ItemKind::Trait(..) => {
380380
println!("trait decl");
381-
if cx.tcx.trait_is_auto(did) {
381+
if cx.tcx.trait_is_auto(did.to_def_id()) {
382382
println!("trait is auto");
383383
} else {
384384
println!("trait is not auto");

tests/ui/issue_2356.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: this loop could be written as a `for` loop
2-
--> $DIR/issue_2356.rs:15:29
2+
--> $DIR/issue_2356.rs:15:9
33
|
44
LL | while let Some(e) = it.next() {
5-
| ^^^^^^^^^ help: try: `for e in it { .. }`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for e in it`
66
|
77
note: the lint level is defined here
88
--> $DIR/issue_2356.rs:1:9

tests/ui/while_let_on_iterator.fixed

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::while_let_on_iterator)]
4+
#![allow(clippy::never_loop, unreachable_code, unused_mut)]
5+
6+
fn base() {
7+
let mut iter = 1..20;
8+
for x in iter {
9+
println!("{}", x);
10+
}
11+
12+
let mut iter = 1..20;
13+
for x in iter {
14+
println!("{}", x);
15+
}
16+
17+
let mut iter = 1..20;
18+
for _ in iter {}
19+
20+
let mut iter = 1..20;
21+
while let None = iter.next() {} // this is fine (if nonsensical)
22+
23+
let mut iter = 1..20;
24+
if let Some(x) = iter.next() {
25+
// also fine
26+
println!("{}", x)
27+
}
28+
29+
// the following shouldn't warn because it can't be written with a for loop
30+
let mut iter = 1u32..20;
31+
while let Some(_) = iter.next() {
32+
println!("next: {:?}", iter.next())
33+
}
34+
35+
// neither can this
36+
let mut iter = 1u32..20;
37+
while let Some(_) = iter.next() {
38+
println!("next: {:?}", iter.next());
39+
}
40+
41+
// or this
42+
let mut iter = 1u32..20;
43+
while let Some(_) = iter.next() {
44+
break;
45+
}
46+
println!("Remaining iter {:?}", iter);
47+
48+
// or this
49+
let mut iter = 1u32..20;
50+
while let Some(_) = iter.next() {
51+
iter = 1..20;
52+
}
53+
}
54+
55+
// Issue #1188
56+
fn refutable() {
57+
let a = [42, 1337];
58+
let mut b = a.iter();
59+
60+
// consume all the 42s
61+
while let Some(&42) = b.next() {}
62+
63+
let a = [(1, 2, 3)];
64+
let mut b = a.iter();
65+
66+
while let Some(&(1, 2, 3)) = b.next() {}
67+
68+
let a = [Some(42)];
69+
let mut b = a.iter();
70+
71+
while let Some(&None) = b.next() {}
72+
73+
/* This gives “refutable pattern in `for` loop binding: `&_` not covered”
74+
for &42 in b {}
75+
for &(1, 2, 3) in b {}
76+
for &Option::None in b.next() {}
77+
// */
78+
}
79+
80+
fn nested_loops() {
81+
let a = [42, 1337];
82+
let mut y = a.iter();
83+
loop {
84+
// x is reused, so don't lint here
85+
while let Some(_) = y.next() {}
86+
}
87+
88+
let mut y = a.iter();
89+
for _ in 0..2 {
90+
while let Some(_) = y.next() {
91+
// y is reused, don't lint
92+
}
93+
}
94+
95+
loop {
96+
let mut y = a.iter();
97+
for _ in y {
98+
// use a for loop here
99+
}
100+
}
101+
}
102+
103+
fn issue1121() {
104+
use std::collections::HashSet;
105+
let mut values = HashSet::new();
106+
values.insert(1);
107+
108+
while let Some(&value) = values.iter().next() {
109+
values.remove(&value);
110+
}
111+
}
112+
113+
fn issue2965() {
114+
// This should not cause an ICE and suggest:
115+
//
116+
// for _ in values.iter() {}
117+
//
118+
use std::collections::HashSet;
119+
let mut values = HashSet::new();
120+
values.insert(1);
121+
122+
for _ in values.iter() {
123+
// FIXME(flip1995): Linting this with the following line uncommented is a FP, see #1654
124+
// values.remove(&1);
125+
}
126+
}
127+
128+
fn issue3670() {
129+
let array = [Some(0), None, Some(1)];
130+
let mut iter = array.iter();
131+
132+
while let Some(elem) = iter.next() {
133+
let _ = elem.or_else(|| *iter.next()?);
134+
}
135+
}
136+
137+
fn main() {
138+
base();
139+
refutable();
140+
nested_loops();
141+
issue1121();
142+
issue2965();
143+
issue3670();
144+
}

0 commit comments

Comments
 (0)