This repository contains crates and tools to help with the removal of the get
prefix from getters in existing Rust code.
Rust naming conventions for getter methods stipulates to use:
A method
foo(&self) -> &T
for getting the current value of the field.
Attempts at removing the get
prefix automatically or manually proved to be
suboptimal with large code bases:
- Some
get_*
functions actually perform some sort of computation, so we can't exactly consider they return the current value of a field. - Removing the
get
prefix automatically can result in invalid code. Ex.get_mut
,get_loop
, ... - Getters returning a
bool
should usually use the formis_suffix
, but sometimes, we just want to conjugate a verb. E.g.:if element.emits_eos()
. - Most
get_*
functions that return abool
and which actually perform some sort of computation can still be renamed asis_suffix
, since it makes sense in expressions such asif context_a.is_same_as(&context_b)
. - Then it's necessary to update the getters call sites and we have to guess the return type somewhat.
Not all get
functions should be renamed. Besides the rules
enforced while renaming the function, other criteria are observed before
deciding whether to apply the new name. get
functions returning exactly one
bool
require special attention since they are usually renamed using an is
prefix or a conjugated verb which make the new name suitable in expressions.
By default, all get
functions are renamed. However, users might prefer sticking
by the definition and only rename getters which actually return a field.
The following rules apply to get
functions not returning exactly one bool
.
They are enforced by the tools when the conservative
mode is selected. Note
that this might leave behind candidates. Use this if you consider it easier to
manually change these candidates after the automatic pass than searching in the
automatic changes.
- The function is a method. This excludes standalone functions or associated functions.
- The function accepts no arguments besides
&['_][mut] self
. The methods which accept other arguments are notgetter
s in the sense that they usually don't return current value of a field. Similarly, functions consumingself
are not eligible forget
removal. - The function accepts no type parameter (lifetimes are accepted). The reason is the same as for functions accepting multiple arguments (see above).
These rules are implemented in the fix-def & fix-calls tools and apply to regular code, macros and documentation code.
This workspace contains the following packages:
- rules: rules to apply during the update process.
- fix-def:
a tool to update the
get
functions definition. - fix-calls:
a tool to update the
get
functions call sites. - utils:
utilities and types common to the
fix-getters
tools.
All crates contained in here are licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.