-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(format_push_string): give a (possibly incomplete) suggestion #16093
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
base: master
Are you sure you want to change the base?
Conversation
|
rustbot has assigned @samueltardieu. Use |
278b57f to
5ea08dd
Compare
|
Lintcheck changes for 9941c97
This comment will be updated if you push new changes |
| // TODO: Ideally we'd use `TyCtxt::in_scope_traits` to detect whether the trait is imported, and | ||
| // either not emit this note if it is, or emit an automated suggestion to import it if it isn't. | ||
| // But the method doesn't seem to work, see https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/How+to+suggest+importing+a+path/with/544260181 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't say that the method doesn't seem to work if you call it outside of its intended use. Given its lack of documentation, it is hard to say. If you want to investigate, you might be luckier asking in a compiler-related Zulip channel.
| // TODO: Ideally we'd use `TyCtxt::in_scope_traits` to detect whether the trait is imported, and | |
| // either not emit this note if it is, or emit an automated suggestion to import it if it isn't. | |
| // But the method doesn't seem to work, see https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/How+to+suggest+importing+a+path/with/544260181 | |
| // TODO: omit the note if the `Write` trait is imported at point |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After more research, I found rust-lang/rust#54161 (comment), which is almost correct (or maybe was correct initially, has become incorrect since) -- the actual behaviour TyCtxt::in_scope_traits seems to be the following: it returns a non-empty list only when called on the HirId of a ExprKind::MethodCall that is a call of a trait method.
Added a note about this
| // TODO: Ideally we'd use `TyCtxt::in_scope_traits` to detect whether the trait is imported, and | ||
| // either not emit this note if it is, or emit an automated suggestion to import it if it isn't. | ||
| // But the method doesn't seem to work, see https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/How+to+suggest+importing+a+path/with/544260181 | ||
| diag.note(format!("you may need to import `{std_or_core}::fmt::Write`")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| diag.note(format!("you may need to import `{std_or_core}::fmt::Write`")); | |
| diag.note(format!("you may need to import the `{std_or_core}::fmt::Write` trait")); |
| let Some(std_or_core) = std_or_core(cx) else { | ||
| // This can't really happen, as a no-core crate wouldn't have access to `String` in the first place | ||
| return; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might happen though:
Example:
#![feature(no_core)]
#![no_core]
#![no_std]
extern crate std as mystd;
use mystd::convert::From as _;
fn main() {
let mut s = mystd::string::String::from("no core, no std");
s.push_str(&mystd::format!("{}", 1+2));
mystd::println!("s = {s}");
}| let Some(std_or_core) = std_or_core(cx) else { | |
| // This can't really happen, as a no-core crate wouldn't have access to `String` in the first place | |
| return; | |
| }; | |
| let Some(std_or_core) = std_or_core(cx) else { | |
| return; | |
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Added a test
|
Reminder, once the PR becomes ready for a review, use |
5ea08dd to
9941c97
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
Resolves #8824
Supersedes #15828 -- I wasn't able to get
TyCtxt::in_scope_traitsto work, so here's a more basic implementation that just adds a note about the possible need to importstd::fmt::Write.changelog: [
format_push_string]: add a suggestion