rust-lang/rust-clippy

Lint non-expressive variable names

Open

#644 opened on Feb 9, 2016

View on GitHub
 (8 comments) (0 reactions) (1 assignee)Rust (10,406 stars) (1,391 forks)batch import
A-lintL-styleT-ASTT-middlegood first issue

Description

So, I've recently had to deal with code written in ▊▊▊▊▊ and for some reason the original authors of the software tended to use either one-letter variables or weirdly long names (that were not even very expressive). Now, don't get me wrong: I don't want to tell anyone how to name their bindings in Rust, I just want to suggest a few pointers.

I'm proposing a lint that suggests (very nicely) to the author to rethink the naming of bindings whose names fall in at least one of the following categories. This lint can, of course, only be a very subjective heuristic (which is basically the worst kind of lint there is), so I'll try to stay conservative.

  • The name contains the name of the type

e.g. let people_vec = vec![]

This is helpful in dynamically typed languages, but less so in a Rust which is statically typed. One difficulty might be the usage of abbreviations for types (or the opposite, like people_vector which is a Vec.

I think some prefixes (like is_ for bools) are okay, though.

  • There are more than 3 one-letter names in the same scope

x, y, z I can understand, but what do a, s, f, and l stand for? Done in #727.

  • The name is more than 25 letters long

This is 1/4 of the usual line width. I've seen many such names that seem to be written that way only to allow the developer to not add documentation/comments in good conscience, since "the code is so obvious". I don't think that is very helpful (variable names describe the implementation, but probably don't help you grasp the bigger picture).


This should probably be Allow by default. I can also imagine this being split up into multiple sub-lints (or as a separate lint group). I don't want to call this a lint for "bad naming". We should probably call it "checking for non-expressive names".

What do you think of this? Is this something you want to do? Is this too opinionated? Do you have other suggestions on how to detect non-expressive names?


Please note that I'm focussing on the naming of variables/bindings, not methods or functions. Conventions for method names (e.g. in builders with/without set and get prefixes) should be discussed in another issue :smile:

Contributor guide