Skip to content

Fixed number after underscore in camel case #13109

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

Closed
wants to merge 2 commits into from
Closed

Fixed number after underscore in camel case #13109

wants to merge 2 commits into from

Conversation

bvssvni
Copy link

@bvssvni bvssvni commented Mar 23, 2014

Closes #12986

fn invalid_following_underscore(ident: &str) -> bool {
for i in range(0, ident.char_len() - 1) {
if ident.char_at(i) != '_' { continue; }
if !"_0123456789".contains_char(ident.char_at(i + 1)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't doing what you expect: it's getting the character after the i + 1th byte, where i is the count of the current character (i.e. not it's byte index, and so will break on an multibyte codepoints) and similarly the char_at above is wrong too. I think this loop could be written correctly as:

let last_was_underscore = false;
for c in ident.chars() {
    if last_was_underscore && !"_0123456789".contains_char(c) {
        return true;
    }
    last_was_underscore = c == '_';
}
false

(untested)

Also, this changes the behaviour to reject _A, where as that was ok previously.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. I had a working algorithm in the issue, but yours is better. I have an older incorrect version in this pull request.

@bvssvni bvssvni closed this Mar 24, 2014
@emberian emberian reopened this Mar 25, 2014
@alexcrichton
Copy link
Member

I'm a little worried about adding exceptions to our stylistic lints. It's pretty easy to ignore them via an allow attribute, and once we start adding special cases it's unknown where we draw the line.

@bvssvni, do you know if there's precedent for this sort of exception? Googling around for camel case I didn't find anything related to this. Is there a reason that an allow attribute doesn't suffice for you?

@bvssvni
Copy link
Author

bvssvni commented Mar 26, 2014

The issue here is code generation.

This case was discovered when I worked on documenting Cairo bindings https://github.com/jensnockert/cairo.rs/search?q=SVGVersion&ref=cmdform. The code is generated from XML files using a Ruby script. It requires a check in these particular places and inject the attribute if one wants to keep the library idiomatic Rust elsewhere.

@alexcrichton
Copy link
Member

I've found that bindings to other systems which do not necessarily repsect Rust's style often must opt-out of the stylistic lints. For example, all bindings to windows functions have to opt-out of the lints because they use camel case for variables and often uppercase variables. Additionally, LLVM has quite different style than rust does.

I would imagine that an #[allow] at the top of the generated bindings would be sufficient for the entire block of generated code?

@bvssvni
Copy link
Author

bvssvni commented Mar 26, 2014

Yes, that would work. It is not a major problem for me.

@bvssvni
Copy link
Author

bvssvni commented Mar 26, 2014

I did some searching and could not find any precedence for this. The tools that convert from underscore to camel case either removes numbers or merges them together, which means there is no conventions to write multiple numbers in this style. It is easier to add those exceptions later than removing them.

I talked to somebody on the rust-internals IRC channel. One was ok with it (if precedence) and one was against. Nobody was strongly in favor of it, neither am I.

Closing.

@bvssvni bvssvni closed this Mar 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow Numbers After Underscore in Camel Case Types
4 participants