Skip to content
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

testsuite: Add test for #4208 #9233

Closed
wants to merge 1 commit into from

Conversation

catamorphism
Copy link
Contributor

Closes #4208

bors added a commit that referenced this pull request Sep 17, 2013
@bors bors closed this Sep 17, 2013
Jarcho pushed a commit to Jarcho/rust that referenced this pull request Aug 29, 2022
Refactor `FormatArgsExpn`

It now for each format argument `{..}` has:
- The `Expr` it points to, and how it does so (named/named inline/numbered/implicit)
- The parsed `FormatSpec` (format trait/fill/align/etc., the precision/width and any value they point to)
- Many spans

The caller no longer needs to pair up arguments to their value, or separately interpret the `specs` `Expr`s when it isn't `None`

The gist is that it combines the result of [`rustc_parse_format::Parser`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse_format/struct.Parser.html) with the macro expansion itself

This unfortunately makes the code a bit longer, however we need to use both as neither have all the information we're after. `rustc_parse_format` doesn't have the information to resolve named arguments to their values. The macro expansion doesn't contain whether the positions are implicit/numbered/named, or the spans for format arguments

Wanted by rust-lang#9233 and rust-lang#8518 to be able to port the changes from rust-lang#9040

Also fixes rust-lang#8643, previously the format args seem to have been paired up with the wrong values somehow

changelog: [`format_in_format_args`]: Fix false positive due to misattributed arguments

r? `@flip1995`
cc `@nyurik`
flip1995 pushed a commit to flip1995/rust that referenced this pull request Oct 6, 2022
new uninlined_format_args lint to inline explicit arguments

Implement rust-lang/rust-clippy#8368 - a new lint to inline format arguments such as `print!("{}", var)` into `print!("{var}")`.

### Supported cases

code | suggestion | comment
---|---|---
`print!("{}", var)` | `print!("{var}")` |  simple variables
`print!("{0}", var)` | `print!("{var}")` |  positional variables
`print!("{v}", v=var)` | `print!("{var}")` |  named variables
`print!("{0} {0}", var)` | `print!("{var} {var}")` |  aliased variables
`print!("{0:1$}", var, width)` | `print!("{var:width$}")` |  width support
`print!("{0:.1$}", var, prec)` | `print!("{var:.prec$}")` |  precision support
`print!("{:.*}", prec, var)` | `print!("{var:.prec$}")` |  asterisk support

### Known Problems

* There may be a false positive if the format string is wrapped in a macro call:
```rust
# let var = 42;
macro_rules! no_param_str { () => { "{}" }; }
macro_rules! pass_through { ($expr:expr) => { $expr }; }
println!(no_param_str!(), var);
println!(pass_through!("{}"), var);
```

* Format string uses an indexed argument that cannot be inlined.
Supporting this case requires re-indexing of the format string.
Until implemented, `print!("{0}={1}", var, 1+2)` should be changed to `print!("{var}={0}", 1+2)` by hand.

changelog: [`uninlined_format_args`]: A new lint to inline format arguments, i.e. `print!("{}", var)` into `print!("{var}")`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE with trait inheritance/generics
3 participants