Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

add special handling for use suggestions #37

Closed
wants to merge 1 commit into from

Conversation

Dushistov
Copy link

comment #36

Copy link
Collaborator

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

Also needs a test.


/// We need some special care for use suggestions,
/// to eliminate duplicates, for example for HashMap,
/// rustc suggests 4 variants:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should not be eliminating suggestions. If there are exact duplicates that's fine, but if we'd eliminate the duplicate for something like Iter, we're basically guaranteed to get the wrong one.

.map(|v| {
let new_v = Suggestion {
line_range: LineRange {
start: LinePosition { line: 1, column: 1},
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should not be hacking around with the insertion span. Let's fix rust-lang/rust#42835 instead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, until we get that fixed, let's just leave this as it is. It's better that to be completely broken

Copy link
Member

@killercup killercup left a comment

Choose a reason for hiding this comment

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

Thanks! In addition to Oliver's comments, I wrote a bit (okay, a lot) on detecting the suggestions by its error code. I'd love for this to become a template to how we can filter specific suggestions. Let me know what you think. If you don't want to/have to time to change this, that's fine too :)

@@ -41,6 +41,12 @@ pub struct Suggestion {
pub replacement: String,
}

impl Suggestion {
pub fn is_use_suggestion(&self) -> bool {
self.replacement.starts_with("use ") && self.replacement.ends_with(";\n")
Copy link
Member

Choose a reason for hiding this comment

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

Can we get a more stable detection here?

Rust's errors have codes that are present in the JSON output like this:

{
    message: {
        children: [{
            code: null, level: "help", message: "", suggested_replacement: "..."
        }],
        code: {
            code: "E0425", explanation: "..."
        },
        level: "error",
        message: "...",
        spans: []
    }
}

So, here, we can probably look for the message.code.code == E0425. This might require adjusting the Suggestions struct.


For future extensions to other lints: Most clippy lint messages start with something like

"children": [
    {
        "children": [],
        "code": null,
        "level": "note",
        "message": "#[warn(needless_borrow)] on by default",
        "rendered": null,
        "spans": []
    },
    {
        "children": [],
        "code": null,
        "level": "help",
        "message": "for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrow",
        "rendered": null,
        "spans": []
    }
]

Parsing the "#[warn(needless_borrow)] on by default" message seems a bit less fragile than "everything that begins with use and ends with a semicolon". It would be even more awesome to get the actual lint name as structured data (maybe even lint group as well as the child lint that actually created the diagnostic message), but that's something that needs changes in rustc (and some discussion, maybe even an RFC).

Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think about placing the lint name into the code field?

Copy link
Member

Choose a reason for hiding this comment

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

As a string or something like "code": { "code": null, "lint": "needless_borrow" }?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd have thought "code": { "code": "needless_borrow", "explanation": "..." }

Copy link
Member

Choose a reason for hiding this comment

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

Sure. I have no idea if error codes and lints ever overlap (i.e., if a lint can have an error code or an error with a code a lint name)

@@ -56,6 +57,9 @@ fn try_main() -> Result<(), ProgramError> {
.arg(Arg::with_name("yolo")
.long("yolo")
.help("Automatically apply all unambiguous suggestions"))
.arg(Arg::with_name("apply-only-use")
.long("apply-only-use")
.help("Apply only use fix suggestions"))
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather we introduce an --apply or --enable or --only flag (can be used multiple times) and, when set, rustfix will filter the given suggestions to only fix these lints.

For now this can be hardcoded to only accept "use" (or whatever the actual lint name is) to keep the code simple.

@killercup
Copy link
Member

Resolved by #42

@killercup killercup closed this Nov 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants