Skip to content

Conversation

@ada4a
Copy link
Contributor

@ada4a ada4a commented Nov 14, 2025

Resolves #8824

Supersedes #15828 -- I wasn't able to get TyCtxt::in_scope_traits to work, so here's a more basic implementation that just adds a note about the possible need to import std::fmt::Write.

changelog: [format_push_string]: add a suggestion

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 14, 2025

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@github-actions
Copy link

github-actions bot commented Nov 14, 2025

Lintcheck changes for 9941c97

Lint Added Removed Changed
clippy::format_push_string 0 0 29

This comment will be updated if you push new changes

Comment on lines 160 to 162
// 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
Copy link
Member

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.

Suggested change
// 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

Copy link
Contributor Author

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`"));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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"));

Comment on lines 138 to 141
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;
};
Copy link
Member

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}");
}
Suggested change
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;
};

Copy link
Contributor Author

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

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Nov 18, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 18, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@ada4a ada4a force-pushed the format_push_string-simple branch from 5ea08dd to 9941c97 Compare November 28, 2025 17:45
@rustbot
Copy link
Collaborator

rustbot commented Nov 28, 2025

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.

@ada4a
Copy link
Contributor Author

ada4a commented Nov 28, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a suggestion for format_push_string

3 participants