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

@ada4a ada4a force-pushed the format_push_string-simple branch from 278b57f to 5ea08dd Compare November 14, 2025 22:55
@github-actions
Copy link

Lintcheck changes for 5ea08dd

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

// 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;
};

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a suggestion for format_push_string

3 participants