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

if x { true } else { false } #12

Closed
kmcallister opened this issue Nov 19, 2014 · 7 comments
Closed

if x { true } else { false } #12

kmcallister opened this issue Nov 19, 2014 · 7 comments

Comments

@kmcallister
Copy link

Not a Rust-specific problem but I've seen it a lot in code from people who are new to programming in general.

Could generalize to find other forms that are better expressed by logical operators.

@llogiq
Copy link
Contributor

llogiq commented May 1, 2015

I think that one should be simple enough to detect, and it's also easy to create a good warning. I'd catch all instances of if { Lit:bool } else { Lit:bool } and suggest a suitable replacement based on the literals.

I'll take a stab at it when I find some free time.

@Manishearth
Copy link
Member

Yep, it's pretty simple. Go ahead! :)

@kmcallister
Copy link
Author

I'd catch all instances of if { Lit:bool } else { Lit:bool } and suggest a suitable replacement based on the literals.

It'd be great to have a general, declarative way to express rules like that. Maybe something similar to Coccinelle.

@Manishearth
Copy link
Member

Technically it's just a single huge match statement (assuming syntax::ptr::P doesn't get in the way) ;)

@kmcallister
Copy link
Author

It would be cool to do this with quotation:

fn check_expr(&mut self, cx: &Context, expr: &Expr) {
    match *expr {
        q!(if $e:expr { $a:lit } else { $b:lit }) => ...

I think this is actually achievable now, if you replace the whole match by an invocation of a procedural macro, similar to html5ever's match_token!.

@llogiq
Copy link
Contributor

llogiq commented May 1, 2015

A macro should be able to generate code to deal with ptr::P, as irritating as it sometimes is. But until we have this macro, I'm going to write the lint the traditional way.

@llogiq
Copy link
Contributor

llogiq commented May 1, 2015

Done. The warnings could give more specific directions (they are a bit abstract for a newbie), but it works, and it's late now; I guess, I'll revisit them later.

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

No branches or pull requests

3 participants