Skip to content

Commit 1ab5842

Browse files
Rollup merge of rust-lang#158222 - Dnreikronos:format/foreign_newline_diagnostic, r=TaKO8Ki
format: ignore println newline in foreign format hints fixes rust-lang#158216 `println!` adds a newline before the unused-arg diagnostic checks for printf-style formats. that made a trailing `%` look like `%` plus ` `, so rustc printed a weird unsupported conversion specifier note. imo the least risky fix is to leave normal format parsing alone and only strip that synthetic newline for the foreign-format hint scan. idk if there is much more to do here, the ui tests cover the reported case plus the older trailing-percent baselines. lgtm to me, ltm this keeps the behavior narrow.
2 parents 75e6a3d + 2a86b91 commit 1ab5842

5 files changed

Lines changed: 37 additions & 15 deletions

File tree

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ fn make_format_args(
607607
// If there's a lot of unused arguments,
608608
// let's check if this format arguments looks like another syntax (printf / shell).
609609
let detect_foreign_fmt = unused.len() > args.explicit_args().len() / 2;
610+
let foreign_fmt_str =
611+
if append_newline { fmt_str.strip_suffix('\n').unwrap_or(fmt_str) } else { fmt_str };
610612
report_missing_placeholders(
611613
ecx,
612614
unused,
@@ -616,7 +618,7 @@ fn make_format_args(
616618
&invalid_refs,
617619
detect_foreign_fmt,
618620
str_style,
619-
fmt_str,
621+
foreign_fmt_str,
620622
uncooked_fmt_str.1.as_str(),
621623
fmt_span,
622624
);

tests/ui/macros/format-foreign-dollar-without-spec.stderr

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ error: argument never used
22
--> $DIR/format-foreign-dollar-without-spec.rs:3:25
33
|
44
LL | println!("%65536$", 1);
5-
| ^ argument never used
5+
| --------- ^ argument never used
6+
| |
7+
| formatting specifier missing
68
|
7-
note: format specifiers use curly braces, and the conversion specifier `
8-
` is unknown or unsupported
9-
--> $DIR/format-foreign-dollar-without-spec.rs:3:15
9+
help: format specifiers use curly braces, consider adding a format specifier
1010
|
11-
LL | println!("%65536$", 1);
12-
| ^^^^^^^^
13-
= note: printf formatting is not supported; see the documentation for `std::fmt`
11+
LL | println!("%65536${}", 1);
12+
| ++
1413

1514
error: aborting due to 1 previous error
1615

tests/ui/macros/issue-92267.stderr

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ error: argument never used
22
--> $DIR/issue-92267.rs:3:34
33
|
44
LL | pub fn main() { println!("🦀%%%", 0) }
5-
| ^ argument never used
5+
| ------- ^ argument never used
6+
| |
7+
| formatting specifier missing
68
|
7-
note: format specifiers use curly braces, and the conversion specifier `
8-
` is unknown or unsupported
9-
--> $DIR/issue-92267.rs:3:30
9+
help: format specifiers use curly braces, consider adding a format specifier
1010
|
11-
LL | pub fn main() { println!("🦀%%%", 0) }
12-
| ^^
13-
= note: printf formatting is not supported; see the documentation for `std::fmt`
11+
LL | pub fn main() { println!("🦀%%%{}", 0) }
12+
| ++
1413

1514
error: aborting due to 1 previous error
1615

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ check-fail
2+
//@ compile-flags: --crate-type=lib
3+
4+
pub fn f(x: f64) {
5+
println!("{x:>8.2}%", "foo");
6+
//~^ ERROR argument never used
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: argument never used
2+
--> $DIR/trailing-percent-format-hint-issue-158216.rs:5:27
3+
|
4+
LL | println!("{x:>8.2}%", "foo");
5+
| ----------- ^^^^^ argument never used
6+
| |
7+
| formatting specifier missing
8+
|
9+
help: format specifiers use curly braces, consider adding a format specifier
10+
|
11+
LL | println!("{x:>8.2}%{}", "foo");
12+
| ++
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)