-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit warning when named arguments are used positionally in format
Addresses Issue 98466 by emitting a warning if a named argument is used like a position argument (i.e. the name is not used in the string to be formatted). Fixes #98466
- Loading branch information
1 parent
c80dde4
commit 1219f72
Showing
10 changed files
with
324 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// check-pass | ||
#![allow(named_arguments_used_positionally)] | ||
|
||
fn main() { | ||
let mut _x: usize; | ||
_x = 1; | ||
println!("_x is {}", _x = 5); | ||
println!("_x is {}", y = _x); | ||
println!("first positional arg {}, second positional arg {}, _x is {}", 1, 2, y = _x); | ||
|
||
let mut _x: usize; | ||
_x = 1; | ||
let _f = format!("_x is {}", _x = 5); | ||
let _f = format!("_x is {}", y = _x); | ||
let _f = format!("first positional arg {}, second positional arg {}, _x is {}", 1, 2, y = _x); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// check-pass | ||
// run-rustfix | ||
|
||
fn main() { | ||
let mut _x: usize; | ||
_x = 1; | ||
println!("_x is {_x}", _x = 5); | ||
//~^ WARNING named argument `_x` is not used by name [named_arguments_used_positionally] | ||
//~| HELP use the named argument by name to avoid ambiguity | ||
println!("_x is {y}", y = _x); | ||
//~^ WARNING named argument `y` is not used by name [named_arguments_used_positionally] | ||
//~| HELP use the named argument by name to avoid ambiguity | ||
println!("first positional arg {}, second positional arg {}, _x is {y}", 1, 2, y = _x); | ||
//~^ WARNING named argument `y` is not used by name [named_arguments_used_positionally] | ||
//~| HELP use the named argument by name to avoid ambiguity | ||
|
||
let mut _x: usize; | ||
_x = 1; | ||
let _f = format!("_x is {_x}", _x = 5); | ||
//~^ WARNING named argument `_x` is not used by name [named_arguments_used_positionally] | ||
//~| HELP use the named argument by name to avoid ambiguity | ||
let _f = format!("_x is {y}", y = _x); | ||
//~^ WARNING named argument `y` is not used by name [named_arguments_used_positionally] | ||
//~| HELP use the named argument by name to avoid ambiguity | ||
let _f = format!("first positional arg {}, second positional arg {}, _x is {y}", 1, 2, y = _x); | ||
//~^ WARNING named argument `y` is not used by name [named_arguments_used_positionally] | ||
//~| HELP use the named argument by name to avoid ambiguity | ||
|
||
let s = "0.009"; | ||
// Confirm that named arguments used in formatting are correctly considered. | ||
println!(".{:0<width$}", s, width = _x); | ||
|
||
let region = "abc"; | ||
let width = 8; | ||
let ls = "abcde"; | ||
let full = "abcde"; | ||
// Confirm that named arguments used in formatting are correctly considered. | ||
println!( | ||
"| {r:rw$?} | {ui:4?} | {v}", | ||
r = region, | ||
rw = width, | ||
ui = ls, | ||
v = full, | ||
); | ||
|
||
// Confirm that named arguments used in formatting are correctly considered. | ||
println!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a = 4); | ||
|
||
// Confirm that named arguments used in formatting are correctly considered. | ||
println!("{:._a$}", "aaaaaaaaaaaaaaaaaa", _a = 4); | ||
} |
Oops, something went wrong.