Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

single_letter_ident #10915

Closed
yoav-lavi opened this issue Jun 9, 2023 · 1 comment · Fixed by #10916
Closed

single_letter_ident #10915

yoav-lavi opened this issue Jun 9, 2023 · 1 comment · Fixed by #10916
Assignees
Labels
A-lint Area: New lints

Comments

@yoav-lavi
Copy link
Contributor

yoav-lavi commented Jun 9, 2023

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.

@yoav-lavi yoav-lavi added the A-lint Area: New lints label Jun 9, 2023
@yoav-lavi yoav-lavi changed the title single_letter_idents single_letter_ident Jun 9, 2023
@Centri3
Copy link
Member

Centri3 commented Jun 9, 2023

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants