Description
What it does
I'd like to suggest a restriction, single_letter_ident
, which forbids identifiers that are a single letter
Advantage
This would allow projects that want to enforce longer identifier names (for readability) to do so without breaking existing projects
Drawbacks
None that I can think of, as it's a restriction which needs to be opted into
Example
impl fmt::Display for MyStruct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_ref())
}
}
Could be written as:
impl fmt::Display for MyStruct {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_ref())
}
}
This would also forbid the following:
struct A {}
enum B {}
trait C {}
fn d() {}
const E: &str = "some const";
static F: &str = "some const";
TBD for enum members / struct fields / assoc. consts / assoc. types, where this makes a bit more sense, e.g.:
struct Point {
x: usize,
y: usize,
}
Generics (e.g. T
) should be allowed to remain single letter idents (IMO, can also be debated)
Alternatively, could be a min_ident_len
lint which has configuration (since 2 letter idents might also be undesirable)
The lint could also optionally (or by configuration) whitelist specific identifiers which are common, e.g. n
for number functions, i
, x
, y
, etc. This could be narrowed to only allow these if they're of specific types to avoid confusion and still restrict the same identifiers for arbitrary cases. TBD whether this is desirable.