Skip to content

Suggest remove deref for type mismatch #107203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2023

Conversation

chenyukang
Copy link
Member

Fixes #106496

@rustbot
Copy link
Collaborator

rustbot commented Jan 22, 2023

r? @compiler-errors

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2023
@compiler-errors
Copy link
Member

There are a few other instances of "consider removing the borrow" in this function that I think use span-to-snippet instead of looking at the HIR. Do you mind investigating those and migrating those too? It's alright if you don't want to.

@compiler-errors
Copy link
Member

@rustbot author

Pls mark as ready-to-review if you don't want to apply the suggestion.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 22, 2023
@chenyukang
Copy link
Member Author

chenyukang commented Jan 23, 2023 via email

@chenyukang
Copy link
Member Author

chenyukang commented Jan 23, 2023

There are a few other instances of "consider removing the borrow" in this function that I think use span-to-snippet instead of looking at the HIR. Do you mind investigating those and migrating those too? It's alright if you don't want to.

I'm not quite understand your meaning, you mean this part?

) if self.can_sub(self.param_env, checked, expected).is_ok() => {
// We have `&T`, check if what was expected was `T`. If so,
// we may want to suggest removing a `&`.
if sm.is_imported(expr.span) {
// Go through the spans from which this span was expanded,
// and find the one that's pointing inside `sp`.
//
// E.g. for `&format!("")`, where we want the span to the
// `format!()` invocation instead of its expansion.
if let Some(call_span) =
iter::successors(Some(expr.span), |s| s.parent_callsite())
.find(|&s| sp.contains(s))
&& sm.is_span_accessible(call_span)
{
return Some((
sp.with_hi(call_span.lo()),
"consider removing the borrow".to_string(),
String::new(),
Applicability::MachineApplicable,
true,
true
));
}
return None;
}
if sp.contains(expr.span)
&& sm.is_span_accessible(expr.span)
{
return Some((
sp.with_hi(expr.span.lo()),
"consider removing the borrow".to_string(),
String::new(),
Applicability::MachineApplicable,
true,
true,
));
}
}

I go through the HIR usage of this function, seems it's necessary since use span-to-snippet to get source code and then search with pattern-matching is not correct in some cases.

let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!("{ident}: "),
None => String::new(),
};
if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Assign(..),
..
})) = self.tcx.hir().find_parent(expr.hir_id)
{
if mutability.is_mut() {
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
return None;
}
}

@compiler-errors
Copy link
Member

Oh, never mind. Ignore this then.

@compiler-errors
Copy link
Member

I think this is fine to merge as-is then.

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 23, 2023

📌 Commit 2aa5555 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 23, 2023
@@ -1233,6 +1233,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sugg_sp = receiver.span;
}
}

if let hir::ExprKind::Unary(hir::UnOp::Deref, ref inner) = expr.kind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to generalize this to multiple "&&" but whatever, this seems like a very common case already!

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 23, 2023
Rollup of 7 pull requests

Successful merges:

 - rust-lang#104926 (Move relationships from FulfillmentContext to Inherited)
 - rust-lang#106854 (Add `Arc::into_inner` for safely discarding `Arc`s without calling the destructor on the inner type.)
 - rust-lang#107108 (Consider doc(alias) when providing typo suggestions)
 - rust-lang#107186 (rustdoc: Use correct pseudo-element selector)
 - rust-lang#107192 (Add myself to the mailmap)
 - rust-lang#107195 (Fix typo in universal_regions.rs comment)
 - rust-lang#107203 (Suggest remove deref for type mismatch)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 66d6a0b into rust-lang:master Jan 23, 2023
@rustbot rustbot added this to the 1.69.0 milestone Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Give better suggestion for reference of Type with trait
4 participants