Skip to content

Commit

Permalink
Auto merge of #5520 - matthiaskrgr:rustup_44, r=flip1995,phansch
Browse files Browse the repository at this point in the history
rustup rust-lang/rust#71215

There's currently an crash in `ui/new_without_default.rs` that I need to figure out how to avoid.

changelog: none
  • Loading branch information
bors committed Apr 25, 2020
2 parents 02c9435 + dda1c8d commit 6ffe725
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 50 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/cognitive_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CognitiveComplexity {
hir_id: HirId,
) {
let def_id = cx.tcx.hir().local_def_id(hir_id);
if !cx.tcx.has_attr(def_id, sym!(test)) {
if !cx.tcx.has_attr(def_id.to_def_id(), sym!(test)) {
self.check(cx, kind, decl, body, span);
}
}
Expand Down
20 changes: 12 additions & 8 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,20 @@ fn check_hash_peq<'a, 'tcx>(
};

span_lint_and_then(
cx, DERIVE_HASH_XOR_EQ, span,
cx,
DERIVE_HASH_XOR_EQ,
span,
mess,
|diag| {
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(impl_id) {
diag.span_note(
cx.tcx.hir().span(hir_id),
"`PartialEq` implemented here"
);
if let Some(local_def_id) = impl_id.as_local() {
let hir_id = cx.tcx.hir().as_local_hir_id(local_def_id);
diag.span_note(
cx.tcx.hir().span(hir_id),
"`PartialEq` implemented here"
);
}
}
});
);
}
});
}
Expand Down Expand Up @@ -225,7 +229,7 @@ fn check_unsafe_derive_deserialize<'a, 'tcx>(
ty: Ty<'tcx>,
) {
fn item_from_def_id<'tcx>(cx: &LateContext<'_, 'tcx>, def_id: DefId) -> &'tcx Item<'tcx> {
let hir_id = cx.tcx.hir().as_local_hir_id(def_id).unwrap();
let hir_id = cx.tcx.hir().as_local_hir_id(def_id.expect_local());
cx.tcx.hir().expect_item(hir_id)
}

Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DocMarkdown {
let headers = check_attrs(cx, &self.valid_idents, &item.attrs);
match item.kind {
hir::ItemKind::Fn(ref sig, _, body_id) => {
if !(is_entrypoint_fn(cx, cx.tcx.hir().local_def_id(item.hir_id))
if !(is_entrypoint_fn(cx, cx.tcx.hir().local_def_id(item.hir_id).to_def_id())
|| in_external_macro(cx.tcx.sess, item.span))
{
lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers, Some(body_id));
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {

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

for node in v.set {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
// If the next item up is a function we check if it is an entry point
// and only then emit a linter warning
let def_id = cx.tcx.hir().local_def_id(parent);
if !is_entrypoint_fn(cx, def_id) {
if !is_entrypoint_fn(cx, def_id.to_def_id()) {
span_lint(cx, EXIT, e.span, "usage of `process::exit`");
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn check_trait_items(cx: &LateContext<'_, '_>, visited_trait: &Item<'_>, trait_i
if cx.access_levels.is_exported(visited_trait.hir_id) && trait_items.iter().any(|i| is_named_self(cx, i, "len")) {
let mut current_and_super_traits = FxHashSet::default();
let visited_trait_def_id = cx.tcx.hir().local_def_id(visited_trait.hir_id);
fill_trait_set(visited_trait_def_id, &mut current_and_super_traits, cx);
fill_trait_set(visited_trait_def_id.to_def_id(), &mut current_and_super_traits, cx);

let is_empty_method_found = current_and_super_traits
.iter()
Expand Down
11 changes: 6 additions & 5 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,20 +576,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
&& !is_iterator_used_after_while_let(cx, iter_expr)
&& !is_nested(cx, expr, &method_args[0]))
{
let iterator = snippet(cx, method_args[0].span, "_");
let mut applicability = Applicability::MachineApplicable;
let iterator = snippet_with_applicability(cx, method_args[0].span, "_", &mut applicability);
let loop_var = if pat_args.is_empty() {
"_".to_string()
} else {
snippet(cx, pat_args[0].span, "_").into_owned()
snippet_with_applicability(cx, pat_args[0].span, "_", &mut applicability).into_owned()
};
span_lint_and_sugg(
cx,
WHILE_LET_ON_ITERATOR,
expr.span,
expr.span.with_hi(match_expr.span.hi()),
"this loop could be written as a `for` loop",
"try",
format!("for {} in {} {{ .. }}", loop_var, iterator),
Applicability::HasPlaceholders,
format!("for {} in {}", loop_var, iterator),
applicability,
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/missing_const_for_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
) {
let def_id = cx.tcx.hir().local_def_id(hir_id);

if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id) {
if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) {
return;
}

// Building MIR for `fn`s with unsatisfiable preds results in ICE.
if fn_has_unsatisfiable_preds(cx, def_id) {
if fn_has_unsatisfiable_preds(cx, def_id.to_def_id()) {
return;
}

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

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

if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) {
if rustc_mir::const_eval::is_min_const_fn(cx.tcx, def_id) {
if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id.to_def_id(), &mir) {
if rustc_mir::const_eval::is_min_const_fn(cx.tcx, def_id.to_def_id()) {
cx.tcx.sess.span_err(span, &err);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/missing_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
};

if let Some(trait_def_id) = trait_def_id {
if cx.tcx.hir().as_local_hir_id(trait_def_id).is_some() && !cx.access_levels.is_exported(impl_item.hir_id) {
if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.hir_id) {
// If a trait is being implemented for an item, and the
// trait is not exported, we don't need #[inline]
return;
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
} = {
let mut ctx = MovedVariablesCtxt::default();
cx.tcx.infer_ctxt().enter(|infcx| {
euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id, cx.param_env, cx.tables).consume_body(body);
euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id.to_def_id(), cx.param_env, cx.tables)
.consume_body(body);
});
ctx
};
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
let mut impls = HirIdSet::default();
cx.tcx.for_each_impl(default_trait_id, |d| {
if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() {
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(ty_def.did) {
impls.insert(hir_id);
if let Some(local_def_id) = ty_def.did.as_local() {
impls.insert(cx.tcx.hir().as_local_hir_id(local_def_id));
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ fn print_item(cx: &LateContext<'_, '_>, item: &hir::Item<'_>) {
},
hir::ItemKind::Trait(..) => {
println!("trait decl");
if cx.tcx.trait_is_auto(did) {
if cx.tcx.trait_is_auto(did.to_def_id()) {
println!("trait is auto");
} else {
println!("trait is not auto");
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issue_2356.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: this loop could be written as a `for` loop
--> $DIR/issue_2356.rs:15:29
--> $DIR/issue_2356.rs:15:9
|
LL | while let Some(e) = it.next() {
| ^^^^^^^^^ help: try: `for e in it { .. }`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for e in it`
|
note: the lint level is defined here
--> $DIR/issue_2356.rs:1:9
Expand Down
144 changes: 144 additions & 0 deletions tests/ui/while_let_on_iterator.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// run-rustfix

#![warn(clippy::while_let_on_iterator)]
#![allow(clippy::never_loop, unreachable_code, unused_mut)]

fn base() {
let mut iter = 1..20;
for x in iter {
println!("{}", x);
}

let mut iter = 1..20;
for x in iter {
println!("{}", x);
}

let mut iter = 1..20;
for _ in iter {}

let mut iter = 1..20;
while let None = iter.next() {} // this is fine (if nonsensical)

let mut iter = 1..20;
if let Some(x) = iter.next() {
// also fine
println!("{}", x)
}

// the following shouldn't warn because it can't be written with a for loop
let mut iter = 1u32..20;
while let Some(_) = iter.next() {
println!("next: {:?}", iter.next())
}

// neither can this
let mut iter = 1u32..20;
while let Some(_) = iter.next() {
println!("next: {:?}", iter.next());
}

// or this
let mut iter = 1u32..20;
while let Some(_) = iter.next() {
break;
}
println!("Remaining iter {:?}", iter);

// or this
let mut iter = 1u32..20;
while let Some(_) = iter.next() {
iter = 1..20;
}
}

// Issue #1188
fn refutable() {
let a = [42, 1337];
let mut b = a.iter();

// consume all the 42s
while let Some(&42) = b.next() {}

let a = [(1, 2, 3)];
let mut b = a.iter();

while let Some(&(1, 2, 3)) = b.next() {}

let a = [Some(42)];
let mut b = a.iter();

while let Some(&None) = b.next() {}

/* This gives “refutable pattern in `for` loop binding: `&_` not covered”
for &42 in b {}
for &(1, 2, 3) in b {}
for &Option::None in b.next() {}
// */
}

fn nested_loops() {
let a = [42, 1337];
let mut y = a.iter();
loop {
// x is reused, so don't lint here
while let Some(_) = y.next() {}
}

let mut y = a.iter();
for _ in 0..2 {
while let Some(_) = y.next() {
// y is reused, don't lint
}
}

loop {
let mut y = a.iter();
for _ in y {
// use a for loop here
}
}
}

fn issue1121() {
use std::collections::HashSet;
let mut values = HashSet::new();
values.insert(1);

while let Some(&value) = values.iter().next() {
values.remove(&value);
}
}

fn issue2965() {
// This should not cause an ICE and suggest:
//
// for _ in values.iter() {}
//
use std::collections::HashSet;
let mut values = HashSet::new();
values.insert(1);

for _ in values.iter() {
// FIXME(flip1995): Linting this with the following line uncommented is a FP, see #1654
// values.remove(&1);
}
}

fn issue3670() {
let array = [Some(0), None, Some(1)];
let mut iter = array.iter();

while let Some(elem) = iter.next() {
let _ = elem.or_else(|| *iter.next()?);
}
}

fn main() {
base();
refutable();
nested_loops();
issue1121();
issue2965();
issue3670();
}
Loading

0 comments on commit 6ffe725

Please sign in to comment.