Skip to content

Automatically remove the `get` prefix from getters and deal with functions returning exactly one `bool`.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

fengalin/fix-getters

Repository files navigation

fix-getters CI dependency status

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 form is_suffix, but sometimes, we just want to conjugate a verb. E.g.: if element.emits_eos().
  • Most get_* functions that return a bool and which actually perform some sort of computation can still be renamed as is_suffix, since it makes sense in expressions such as if 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.

Get functions selection

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 not getters in the sense that they usually don't return current value of a field. Similarly, functions consuming self are not eligible for get 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.

Packages

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.

LICENSE

All crates contained in here are licensed under either of

at your option.

About

Automatically remove the `get` prefix from getters and deal with functions returning exactly one `bool`.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages