Suggest removing & when awaiting a reference to a future#154933
Suggest removing & when awaiting a reference to a future#154933fru1tworld wants to merge 1 commit intorust-lang:mainfrom
& when awaiting a reference to a future#154933Conversation
f36f2b9 to
5afb519
Compare
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| && self | ||
| .type_implements_trait(future_trait, [inner_ty], obligation.param_env) | ||
| .must_apply_modulo_regions() | ||
| { |
There was a problem hiding this comment.
maybe you want to use span_suggestion_verbose with Applicability::MaybeIncorrect.
| && let future_trait = | ||
| self.tcx.require_lang_item(LangItem::Future, obligation.cause.span) | ||
| && self | ||
| .type_implements_trait(future_trait, [inner_ty], obligation.param_env) |
There was a problem hiding this comment.
This branch should not suggest removing & based only on inner_ty: Future. That also matches &dyn Future, where removing & is not a valid fix. Please reuse the existing span-based borrow-removal logic (see suggest_remove_reference or extract it into a helper) and only emit the fix-it when there is an actual borrow in the source.
test code for this is like:
async fn ref_param(fut: &impl Future<Output = ()>) {
fut.await;
}
async fn dyn_ref_param(fut: &dyn Future<Output = ()>) {
fut.await;
}There was a problem hiding this comment.
Added !matches!(inner_ty.kind(), ty::Dynamic(..)) to skip &dyn Future and added a test.
bff3e3b to
cdd3efa
Compare
| | | ||
| = help: the trait `Future` is not implemented for `&impl std::future::Future<Output = ()>` | ||
| = note: &impl std::future::Future<Output = ()> must be a future or must implement `IntoFuture` to be awaited | ||
| = help: a reference to a future is not a future; consider removing the leading `&`-reference |
There was a problem hiding this comment.
the check does not works since it still suggesting here.
There was a problem hiding this comment.
Fixed - it now only suggests when there's an actual '&' in the source
bc9d978 to
7e22d2d
Compare
This comment has been minimized.
This comment has been minimized.
7e22d2d to
fe623c3
Compare
| error[E0277]: `&impl Future<Output = ()>` is not a future | ||
| --> $DIR/await-ref-future.rs:9:9 | ||
| | | ||
| LL | fut.await; |
There was a problem hiding this comment.
this test is the code from original issue, now suggest remove & not working for it.
as I referred, you may need to reuse the existing span-based borrow-removal logic (see suggest_remove_reference or extract it into a helper)
|
nit: it's not necessary to request review after each commit, reviewer can receive notification from your git push, and will review it when time available(it's normal to wait several days since a lot of us working in free time). https://rustc-dev-guide.rust-lang.org/contributing.html?highlight=reviewer#waiting-for-reviews
|
fe623c3 to
9aed706
Compare
|
Updated to handle the additional cases:
|
Fixes #87211
When
.awaiting&impl Future, suggest removing the&instead of removing.await.