google / rerast

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

Rerast

Rerast is a search/replace tool for Rust code using rules. A rule consists of a search pattern, a replacement and possibly some placeholders that can appear in both the search pattern and the replacement. Matching is done on syntax, not on text, so formatting doesn't matter. Placeholders are typed and must match the type found in the code for the rule to apply.

Rerast is deprecated. We suggest using the Structured Search Replace feature available in rust-analyzer. It is available either in vscode or from the command line (and possibly also vim). If you are missing any particular feature that Rerast supported (or didn't), please comment on this issue.

If you'd like to still use Rerast, we suggest using Rerast version 0.1.88 with Rust version nightly-2020-02-27. There are a few newer version of Rerast, but there are some broken features in the newer versions.

Installation

rustup toolchain add nightly-2020-02-27
rustup component add --toolchain nightly-2020-02-27 rustc-dev
cargo +nightly-2020-02-27 install --version 0.1.88 rerast

Usage

Basic operations can be performed entirely from the command line

cargo +nightly-2020-02-27 rerast --placeholders 'a: i32' --search 'a + 1' --replace_with 'a - 1' --diff

Alternatively you can put your rule in a Rust file

fn rule1(a: i32) {
  replace!(a + 1 => a - 1);
}

then use

cargo +nightly-2020-02-27 rerast --rules_file=my_rules.rs

Putting your rules in a file is required if you want to apply multiple rules at once.

If you'd like to actually update your files, that can be done as follows:

cargo +nightly-2020-02-27 rerast --placeholders 'a: i32' --search 'a + 1' --replace_with 'a - 1' --force --backup

You can control which compilation roots rerast will inject the rule into using the --file argument, e.g.:

cargo +nightly-2020-02-27 rerast --rules_file=my_rules.rs --targets tests --file tests/testsuite/main.rs --diff

Here's a more complex example

use std::rc::Rc;
fn rule1<T>(r: Rc<T>) {
  replace!(r.clone() => Rc::clone(&r))
}

Here we're replacing calls to the clone() method on an Rc with the more explicit way of cloning an Rc - via Rc::clone.

"r" is a placeholder which will match any expression of the type specified. The name of the function "rule1" is not currently used for anything. In future it may be possible to selectively enable/disable rules by specifying their name, so it's probably a good idea to put a slightly descriptive name here. Similarly, comments placed before the function may in the future be displayed to users when the rule matches. This is not yet implemented.

A function can contain multiple invocations of the replace! macro, with earlier rules taking precedence. This is useful if you want to do several replacements that make use of the same placeholders or if you want to handle certain special patterns first, ahead of a more general match.

Besides replace! there are several other replacement macros that can be used:

Replacing statements is currently disabled pending a good use-case.

Matching macro invocations

Macro invocations can be matched so long as they expand to code that can be matched. Note however that a macro invocation will not match against the equivalent code, nor the invocation of a different, but identical macro. This is intentional. When verifying a match, we check that the same sequence of expansions was followed. Also note, that if a macro expands to something different every time it is invoked, it will never match. println! is an example of such a macro, since it generates a constant that is referenced from the expanded code and every invocation references a different constant.

Order of operations

Suppose you're replacing foo(a, b) with a && !b. Depending on what the placeholders end up matching and what context the entire expression is in, there may be need for extra parenthesis. For example if the matched code was !foo(x == 1, y == 2), if we didn't add any parenthesis, we'd end up with !x == 1 && !y == 2 which clearly isn't correct. Rerast detects this and adds parenthesis as needed in order to preserve the order or precedence found in the replacement. This would give !(x == 1 && !(y == 2)).

Formatting of code

No reformatting of code is currently done. Unmatched code will not be affected. Replacement code is produced by copying the replacement code from the rule and splicing in any matched patterns. In future, we may adjust identation for multi-line replacements. Running rustfmt afterwards is probably a good idea since some identation and line lengths may not be ideal.

Recursive and overlapping matches

The first matched rule wins. When some code is matched, no later rules will be applied to that code. However, code matched to placeholders will be searched for further matches to all rules.

Automatically determining a rule from a source code change

If you're about to make a change multiple times throughout your source code and you're using git, you can commit (or stage) your changes, make one edit then run:

cargo +nightly-2020-02-27 rerast --replay_git --diff

This will locate the changed expression in your project (of which there should be only one) then try to determine a rule that would have produced this change. It will print the rule, then apply it to your project. If you are happy with the changes, you can run again with --force to apply them, or you could copy the printed rule into a .rs file and apply it with --rules_file.

Limitations

Known issues

More examples

See the Rerast Cookbook for more examples.

Groups

Questions?

Feel free to just file an issue on github.

Authors

See Cargo.toml

Contributing

See CONTRIBUTING.md

Code of conduct

This project defers to the Rust code of conduct. If you feel someone is not adhering to the code of conduct in relation to this project, please contact David Lattimore. My email address is in Cargo.toml.

Disclaimer

This is not an official Google product. It's released by Google only because the (original) author happens to work there.