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

Improve suggestions #1060

Merged
merged 32 commits into from
Jul 9, 2016
Merged

Improve suggestions #1060

merged 32 commits into from
Jul 9, 2016

Conversation

mcarton
Copy link
Member

@mcarton mcarton commented Jun 29, 2016

Fix #1052.
This is a work in progress. I probably have forgotten most suggestions. I'll review them all tomorrow.

ONE == 1f32; //~ERROR ==-comparison of f32 or f64
ONE == (1.0 + 0.0); //~ERROR ==-comparison of f32 or f64

ONE + ONE == (ZERO + ONE + ONE); //~ERROR ==-comparison of f32 or f64
Copy link
Member Author

Choose a reason for hiding this comment

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

Feeling like cheating anyone?

@mcarton
Copy link
Member Author

mcarton commented Jul 1, 2016

(This is now based on #1054.)

Some more better suggestions. Introducing multispan_sugg, a function to build a suggestion made from several replacements, without playing with spans manually.
Still a WIP, there are much more suggestions to review.

@mcarton
Copy link
Member Author

mcarton commented Jul 6, 2016

Ok, this PR has become big enough. I think it's ready to be merged.

};

let hint = if ret {
format!("return {};", snip)
Copy link
Contributor

Choose a reason for hiding this comment

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

The semicolon is wrong here, if the if-then-else is a match arm (or a function argument or sth)

@Manishearth
Copy link
Member

I've had a broad look and am very much 🎉 🎈 🎆 for this, but haven't had the time for a thorough review, yet. Thanks for doing this, though!

@oli-obk
Copy link
Contributor

oli-obk commented Jul 6, 2016

I'm through. The Sugg struct is awesome. A lot of code became more readable. Especially where the ad-hoc parenthesis adders were factored out.

Some parts are a little confusing, like sugg.as_ty(&format!("{} as {}", x, y)). I get that the "type" is just a string, so we can dump anything there, and that there's not much advantage over sugg.as_ty(x.to_string()).as_ty(y.to_string()), but it feels weird. But it's not a blocker imo.

Except for the needless bool refactoring, I could not find any bugs that were introduced from the refactoring.

@mcarton
Copy link
Member Author

mcarton commented Jul 6, 2016

Some parts are a little confusing, like sugg.as_ty(&format!("{} as {}", x, y)). I get that the "type" is just a string, so we can dump anything there, and that there's not much advantage over sugg.as_ty(x.to_string()).as_ty(y.to_string()), but it feels weird. But it's not a blocker imo.

But sugg.as_ty(x.to_string()).as_ty(y.to_string()) is 17% longer as sugg.as_ty(&format!("{} as {}", x, y))! 😅

Except for the needless bool refactoring, I could not find any bugs that were introduced from the refactoring.

Let's see what rustfmt suggests about that:

% cat a.rs
fn foo() -> i32 {
    return match 42 {
        1337 => 314,
        _ => 0xdeadbeef,
    }
}
% rustfmt a.rs
% cat a.rs
fn foo() -> i32 {
    return match 42 {
        1337 => 314,
        _ => 0xdeadbeef,
    };
}
% cat b.rs
fn foo() -> i32 {
    return if true {
        42
    } else {
        1337
    }
}
% rustfmt b.rs
% cat b.rs
fn foo() -> i32 {
    return if true {
        42
    } else {
        1337
    };
}

I think the convention is to always have a semi-colon with an explicit return.

@Manishearth
Copy link
Member

@nrc Btw, this API might be useful for a rustfix tool in the future.

@Manishearth
Copy link
Member

Theoretically we could tweak this API so that it can actually fix the code by editing it directly! 😄

I've always wanted such a mode for rustc and clippy. Still involves some work, but this PR gets most of it done

@oli-obk
Copy link
Contributor

oli-obk commented Jul 6, 2016

I meant match x { a => return foo, _ => {} }

@oli-obk
Copy link
Contributor

oli-obk commented Jul 6, 2016

is 17% longer

cough make the method generic over Display cough

@mcarton
Copy link
Member Author

mcarton commented Jul 6, 2016

cough make the method generic over Display cough

Duh me.

I meant match x { a => return foo, _ => {} }

Oh right!

Theoretically we could tweak this API so that it can actually fix the code by editing it directly! 😄

Needs more works 😄, but that would be great.

@Manishearth
Copy link
Member

And now, https://github.com/killercup/rustfix exists!

@killercup
Copy link
Member

killercup commented Jul 9, 2016

@mcarton, before I open a new issue: Does this change by any chance fix that some suggestions for lines that end with ; do not currently end with a ;?

Just got this one in my rustfix demo:

Info: transmute from a pointer type (`*mut libc::c_void`) to a reference type (`&mut Box<for<'r, 'r> std::ops::FnMut(&'r menus::MenuItem, &'r windows::Window)>`)
      #[warn(transmute_ptr_to_ref)] on by default
  --> src/menus.rs:50:17-51:76
Suggestion - Replace:

    mem::transmute::<*mut c_void,
                     &mut Box<FnMut(&MenuItem, &Window)>>(data)(&menu_item, &window);

with:

    &mut *(data as *mut Box<for<'r, 'r> std::ops::FnMut(&'r menus::MenuItem, &'r windows::Window)>)

(note the missing semicolon in the suggestion).

If not, feel free to copy this comment into a new issue 😄

@mcarton
Copy link
Member Author

mcarton commented Jul 9, 2016

@killercup not yet, the context isn't used. But it should. I've been planning to do a second round with that.

@killercup
Copy link
Member

@mcarton Okay, cool. I'll just paste that into a new issue then :)

Already worked around this with an ugly patch anyway 😄

@mcarton
Copy link
Member Author

mcarton commented Jul 9, 2016

@killercup the thing is not all suggestions might need a ;. Imagine the needless_ret lint might suggest to replace return x; with x.

@killercup
Copy link
Member

@mcarton Right, I totally missed that. Are there other lints that might do that?

I opened #1076, btw.

@mcarton
Copy link
Member Author

mcarton commented Jul 9, 2016

I'm not sure. For that specific example probably not many.
Don't hesitate to report other things like that! I'd really want a “just fix it” tool 🎉 ✨

@llogiq
Copy link
Contributor

llogiq commented Jul 9, 2016

So let's merge this already!

@llogiq llogiq merged commit ad1cd99 into master Jul 9, 2016
@llogiq llogiq deleted the sugg branch July 9, 2016 22:07
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.

5 participants