google / rerast

A tool for transforming Rust code using rules
Apache License 2.0
711 stars 40 forks source link

Matching of string literals #44

Open RReverser opened 4 years ago

RReverser commented 4 years ago

String literals are somewhat special in Rust in that some built-in macros only accept them and not arbitrary &'static str.

I've tried to run:

cargo +nightly rerast --placeholders 's: &str, a: impl std::fmt::Display' --search 'eprintln!(s, a)' --replace_with 'info!(s, a)' --diff

but this fails with

error: error: format argument must be a string literal
 --> src\__rerast_rules.rs:1:68
  |
1 | pub fn rule(s: &str, a: impl std::fmt::Display){replace!(eprintln!(s, a) => info!(s, a));}
  |                                                                    ^
  |
help: you might be missing a string literal to format with
  |
1 | pub fn rule(s: &str, a: impl std::fmt::Display){replace!(eprintln!("{} {}", s, a) => info!(s, a));}
  |                                                                    ^^^^^^^^

because the passed argument is, well, an argument and not an actual literal.

davidlattimore commented 4 years ago

Thanks for the feedback. You're correct that this is something that rerast doesn't currently support. I've started work on a new version of rerast that works very differently than the current version. It should be able to support this. It's still in the early stages though, so is a long way from being usable.

RReverser commented 4 years ago

Sounds good, looking forward to trying it out when it's ready.