Skip to content

Commit b166642

Browse files
authored
Rollup merge of rust-lang#90939 - estebank:wg-af-polish, r=tmandry
Tweak errors coming from `for`-loop, `?` and `.await` desugaring * Suggest removal of `.await` on non-`Future` expression * Keep track of obligations introduced by desugaring * Remove span pointing at method for obligation errors coming from desugaring * Point at called local sync `fn` and suggest making it `async` ``` error[E0277]: `()` is not a future --> $DIR/unnecessary-await.rs:9:10 | LL | boo().await; | -----^^^^^^ `()` is not a future | | | this call returns `()` | = help: the trait `Future` is not implemented for `()` help: do not `.await` the expression | LL - boo().await; LL + boo(); | help: alternatively, consider making `fn boo` asynchronous | LL | async fn boo () {} | +++++ ``` Fix rust-lang#66731.
2 parents 6b6cc5d + c5287b3 commit b166642

13 files changed

+47
-80
lines changed

clippy_lints/src/methods/str_splitn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn parse_iter_usage(
204204
match e.kind {
205205
ExprKind::Call(
206206
Expr {
207-
kind: ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, _)),
207+
kind: ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)),
208208
..
209209
},
210210
_,

clippy_lints/src/needless_late_init.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn contains_assign_expr<'tcx>(cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'tcx>) ->
7373
seen
7474
}
7575

76-
#[derive(Debug)]
76+
#[derive(Debug, Clone)]
7777
struct LocalAssign {
7878
lhs_id: HirId,
7979
lhs_span: Span,
@@ -154,9 +154,14 @@ fn assignment_suggestions<'tcx>(
154154
assignments.push(assign);
155155
}
156156

157-
let suggestions = assignments
157+
let suggestions = assignments.clone()
158158
.into_iter()
159-
.map(|assignment| Some((assignment.span, snippet_opt(cx, assignment.rhs_span)?)))
159+
.map(|assignment| Some((assignment.span.until(assignment.rhs_span), String::new())))
160+
.chain(
161+
assignments
162+
.into_iter()
163+
.map(|assignment| Some((assignment.rhs_span.shrink_to_hi().with_hi(assignment.span.hi()), String::new())))
164+
)
160165
.collect::<Option<Vec<(Span, String)>>>()?;
161166

162167
let applicability = if suggestions.len() > 1 {

clippy_lints/src/needless_question_mark.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
105105
};
106106
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &arg.kind;
107107
if let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind;
108-
if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, _)) = &called.kind;
108+
if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)) = &called.kind;
109109
if expr.span.ctxt() == inner_expr.span.ctxt();
110110
let expr_ty = cx.typeck_results().expr_ty(expr);
111111
let inner_ty = cx.typeck_results().expr_ty(inner_expr);

clippy_lints/src/strings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
260260
if method_names[0] == sym!(as_bytes);
261261

262262
// Check for slicer
263-
if let ExprKind::Struct(QPath::LangItem(LangItem::Range, _), _, _) = right.kind;
263+
if let ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), _, _) = right.kind;
264264

265265
then {
266266
let mut applicability = Applicability::MachineApplicable;

clippy_lints/src/try_err.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
6565
if let ExprKind::Match(match_arg, _, MatchSource::TryDesugar) = expr.kind;
6666
if let ExprKind::Call(match_fun, try_args) = match_arg.kind;
6767
if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
68-
if matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, _));
68+
if matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, ..));
6969
if let Some(try_arg) = try_args.get(0);
7070
if let ExprKind::Call(err_fun, err_args) = try_arg.kind;
7171
if let Some(err_arg) = err_args.get(0);

clippy_lints/src/unused_io_amount.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
4949
if let hir::ExprKind::Call(func, [ref arg_0, ..]) = res.kind {
5050
if matches!(
5151
func.kind,
52-
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryTraitBranch, _))
52+
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryTraitBranch, ..))
5353
) {
5454
check_map_error(cx, arg_0, expr);
5555
}

clippy_lints/src/utils/author.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
260260
}
261261

262262
fn qpath(&self, qpath: &Binding<&QPath<'_>>) {
263-
if let QPath::LangItem(lang_item, _) = *qpath.value {
263+
if let QPath::LangItem(lang_item, ..) = *qpath.value {
264264
out!("if matches!({qpath}, QPath::LangItem(LangItem::{lang_item:?}, _));");
265265
} else {
266266
out!("if match_qpath({qpath}, &[{}]);", path_to_string(qpath.value));

clippy_utils/src/higher.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl<'a> Range<'a> {
218218
hir::ExprKind::Call(path, args)
219219
if matches!(
220220
path.kind,
221-
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, _))
221+
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, ..))
222222
) =>
223223
{
224224
Some(Range {
@@ -228,27 +228,27 @@ impl<'a> Range<'a> {
228228
})
229229
},
230230
hir::ExprKind::Struct(path, fields, None) => match &path {
231-
hir::QPath::LangItem(hir::LangItem::RangeFull, _) => Some(Range {
231+
hir::QPath::LangItem(hir::LangItem::RangeFull, ..) => Some(Range {
232232
start: None,
233233
end: None,
234234
limits: ast::RangeLimits::HalfOpen,
235235
}),
236-
hir::QPath::LangItem(hir::LangItem::RangeFrom, _) => Some(Range {
236+
hir::QPath::LangItem(hir::LangItem::RangeFrom, ..) => Some(Range {
237237
start: Some(get_field("start", fields)?),
238238
end: None,
239239
limits: ast::RangeLimits::HalfOpen,
240240
}),
241-
hir::QPath::LangItem(hir::LangItem::Range, _) => Some(Range {
241+
hir::QPath::LangItem(hir::LangItem::Range, ..) => Some(Range {
242242
start: Some(get_field("start", fields)?),
243243
end: Some(get_field("end", fields)?),
244244
limits: ast::RangeLimits::HalfOpen,
245245
}),
246-
hir::QPath::LangItem(hir::LangItem::RangeToInclusive, _) => Some(Range {
246+
hir::QPath::LangItem(hir::LangItem::RangeToInclusive, ..) => Some(Range {
247247
start: None,
248248
end: Some(get_field("end", fields)?),
249249
limits: ast::RangeLimits::Closed,
250250
}),
251-
hir::QPath::LangItem(hir::LangItem::RangeTo, _) => Some(Range {
251+
hir::QPath::LangItem(hir::LangItem::RangeTo, ..) => Some(Range {
252252
start: None,
253253
end: Some(get_field("end", fields)?),
254254
limits: ast::RangeLimits::HalfOpen,

clippy_utils/src/hir_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl HirEqInterExpr<'_, '_, '_> {
346346
(&QPath::TypeRelative(lty, lseg), &QPath::TypeRelative(rty, rseg)) => {
347347
self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
348348
},
349-
(&QPath::LangItem(llang_item, _), &QPath::LangItem(rlang_item, _)) => llang_item == rlang_item,
349+
(&QPath::LangItem(llang_item, ..), &QPath::LangItem(rlang_item, ..)) => llang_item == rlang_item,
350350
_ => false,
351351
}
352352
}

tests/ui/future_not_send.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ LL | async fn private_future(rc: Rc<[u8]>, cell: &Cell<usize>) -> bool {
66
|
77
= note: `-D clippy::future-not-send` implied by `-D warnings`
88
note: future is not `Send` as this value is used across an await
9-
--> $DIR/future_not_send.rs:8:5
9+
--> $DIR/future_not_send.rs:8:19
1010
|
1111
LL | async fn private_future(rc: Rc<[u8]>, cell: &Cell<usize>) -> bool {
1212
| -- has type `std::rc::Rc<[u8]>` which is not `Send`
1313
LL | async { true }.await
14-
| ^^^^^^^^^^^^^^^^^^^^ await occurs here, with `rc` maybe used later
14+
| ^^^^^^ await occurs here, with `rc` maybe used later
1515
LL | }
1616
| - `rc` is later dropped here
1717
= note: `std::rc::Rc<[u8]>` doesn't implement `std::marker::Send`
1818
note: future is not `Send` as this value is used across an await
19-
--> $DIR/future_not_send.rs:8:5
19+
--> $DIR/future_not_send.rs:8:19
2020
|
2121
LL | async fn private_future(rc: Rc<[u8]>, cell: &Cell<usize>) -> bool {
2222
| ---- has type `&std::cell::Cell<usize>` which is not `Send`
2323
LL | async { true }.await
24-
| ^^^^^^^^^^^^^^^^^^^^ await occurs here, with `cell` maybe used later
24+
| ^^^^^^ await occurs here, with `cell` maybe used later
2525
LL | }
2626
| - `cell` is later dropped here
2727
= note: `std::cell::Cell<usize>` doesn't implement `std::marker::Sync`
@@ -33,12 +33,12 @@ LL | pub async fn public_future(rc: Rc<[u8]>) {
3333
| ^ future returned by `public_future` is not `Send`
3434
|
3535
note: future is not `Send` as this value is used across an await
36-
--> $DIR/future_not_send.rs:12:5
36+
--> $DIR/future_not_send.rs:12:19
3737
|
3838
LL | pub async fn public_future(rc: Rc<[u8]>) {
3939
| -- has type `std::rc::Rc<[u8]>` which is not `Send`
4040
LL | async { true }.await;
41-
| ^^^^^^^^^^^^^^^^^^^^ await occurs here, with `rc` maybe used later
41+
| ^^^^^^ await occurs here, with `rc` maybe used later
4242
LL | }
4343
| - `rc` is later dropped here
4444
= note: `std::rc::Rc<[u8]>` doesn't implement `std::marker::Send`
@@ -82,12 +82,12 @@ LL | async fn private_future(&self) -> usize {
8282
| ^^^^^ future returned by `private_future` is not `Send`
8383
|
8484
note: future is not `Send` as this value is used across an await
85-
--> $DIR/future_not_send.rs:35:9
85+
--> $DIR/future_not_send.rs:35:23
8686
|
8787
LL | async fn private_future(&self) -> usize {
8888
| ----- has type `&Dummy` which is not `Send`
8989
LL | async { true }.await;
90-
| ^^^^^^^^^^^^^^^^^^^^ await occurs here, with `&self` maybe used later
90+
| ^^^^^^ await occurs here, with `&self` maybe used later
9191
LL | self.rc.len()
9292
LL | }
9393
| - `&self` is later dropped here
@@ -100,12 +100,12 @@ LL | pub async fn public_future(&self) {
100100
| ^ future returned by `public_future` is not `Send`
101101
|
102102
note: future is not `Send` as this value is used across an await
103-
--> $DIR/future_not_send.rs:40:9
103+
--> $DIR/future_not_send.rs:40:30
104104
|
105105
LL | pub async fn public_future(&self) {
106106
| ----- has type `&Dummy` which is not `Send`
107107
LL | self.private_future().await;
108-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here, with `&self` maybe used later
108+
| ^^^^^^ await occurs here, with `&self` maybe used later
109109
LL | }
110110
| - `&self` is later dropped here
111111
= note: `std::rc::Rc<[u8]>` doesn't implement `std::marker::Sync`
@@ -117,12 +117,12 @@ LL | async fn generic_future<T>(t: T) -> T
117117
| ^ future returned by `generic_future` is not `Send`
118118
|
119119
note: future is not `Send` as this value is used across an await
120-
--> $DIR/future_not_send.rs:54:5
120+
--> $DIR/future_not_send.rs:54:19
121121
|
122122
LL | let rt = &t;
123123
| -- has type `&T` which is not `Send`
124124
LL | async { true }.await;
125-
| ^^^^^^^^^^^^^^^^^^^^ await occurs here, with `rt` maybe used later
125+
| ^^^^^^ await occurs here, with `rt` maybe used later
126126
LL | t
127127
LL | }
128128
| - `rt` is later dropped here

tests/ui/needless_late_init_fixable.fixed

-38
This file was deleted.

tests/ui/needless_late_init_fixable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// run-rustfix
2-
31
#![allow(unused, clippy::assign_op_pattern)]
42

53
fn main() {

tests/ui/needless_late_init_fixable.stderr

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unneeded late initalization
2-
--> $DIR/needless_late_init_fixable.rs:6:5
2+
--> $DIR/needless_late_init_fixable.rs:4:5
33
|
44
LL | let a;
55
| ^^^^^^
@@ -11,7 +11,7 @@ LL | let a = "zero";
1111
| ~~~~~
1212

1313
error: unneeded late initalization
14-
--> $DIR/needless_late_init_fixable.rs:9:5
14+
--> $DIR/needless_late_init_fixable.rs:7:5
1515
|
1616
LL | let b;
1717
| ^^^^^^
@@ -22,7 +22,7 @@ LL | let b = 1;
2222
| ~~~~~
2323

2424
error: unneeded late initalization
25-
--> $DIR/needless_late_init_fixable.rs:10:5
25+
--> $DIR/needless_late_init_fixable.rs:8:5
2626
|
2727
LL | let c;
2828
| ^^^^^^
@@ -33,7 +33,7 @@ LL | let c = 2;
3333
| ~~~~~
3434

3535
error: unneeded late initalization
36-
--> $DIR/needless_late_init_fixable.rs:14:5
36+
--> $DIR/needless_late_init_fixable.rs:12:5
3737
|
3838
LL | let d: usize;
3939
| ^^^^^^^^^^^^^
@@ -44,7 +44,7 @@ LL | let d: usize = 1;
4444
| ~~~~~~~~~~~~
4545

4646
error: unneeded late initalization
47-
--> $DIR/needless_late_init_fixable.rs:17:5
47+
--> $DIR/needless_late_init_fixable.rs:15:5
4848
|
4949
LL | let mut e;
5050
| ^^^^^^^^^^
@@ -55,7 +55,7 @@ LL | let mut e = 1;
5555
| ~~~~~~~~~
5656

5757
error: unneeded late initalization
58-
--> $DIR/needless_late_init_fixable.rs:21:5
58+
--> $DIR/needless_late_init_fixable.rs:19:5
5959
|
6060
LL | let f;
6161
| ^^^^^^
@@ -66,11 +66,12 @@ LL | let f = match 1 {
6666
| +++++++
6767
help: remove the assignments from the `match` arms
6868
|
69-
LL | 1 => "three",
70-
| ~~~~~~~
69+
LL - 1 => f = "three",
70+
LL + 1 => "three",
71+
|
7172

7273
error: unneeded late initalization
73-
--> $DIR/needless_late_init_fixable.rs:27:5
74+
--> $DIR/needless_late_init_fixable.rs:25:5
7475
|
7576
LL | let g: usize;
7677
| ^^^^^^^^^^^^^
@@ -81,15 +82,16 @@ LL | let g: usize = if true {
8182
| ++++++++++++++
8283
help: remove the assignments from the branches
8384
|
84-
LL | 5
85-
|
85+
LL - g = 5;
86+
LL + 5
87+
|
8688
help: add a semicolon after the `if` expression
8789
|
8890
LL | };
8991
| +
9092

9193
error: unneeded late initalization
92-
--> $DIR/needless_late_init_fixable.rs:34:5
94+
--> $DIR/needless_late_init_fixable.rs:32:5
9395
|
9496
LL | let h;
9597
| ^^^^^^

0 commit comments

Comments
 (0)