Skip to content

Lint for trait methods without bodies #2367

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

Merged
merged 2 commits into from
Jan 19, 2018
Merged

Lint for trait methods without bodies #2367

merged 2 commits into from
Jan 19, 2018

Conversation

etaoins
Copy link
Contributor

@etaoins etaoins commented Jan 17, 2018

As discussed in rust-lang/rust#47475 the #[inline] attribute is currently allowed on trait methods without bodies (i.e. without a default implementation). This is misleading as it could be interpreted
as affecting the implementations of the trait method. Add a lint for any use of #[inline] on a trait method without a body.

Fixes rust-lang/rust#47475

@oli-obk
Copy link
Contributor

oli-obk commented Jan 17, 2018

I think this should some day be caught by the unused_attribute lint from rustc, 'till then, let's lint this special case from clippy

@killercup
Copy link
Member

@oli-obk do we have a mechanisms for suggestions that just remove code? We could use that here.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 17, 2018

Right. Should be as simple as changing the span_lint into a span_lint_and_sugg and using String::new() as the suggestion

@etaoins
Copy link
Contributor Author

etaoins commented Jan 17, 2018

@oli-obk @killercup

This has the downside of only removing the attribute itself, not the line it was on. This is a bit tricky because technically attributes do not need to be on their own line so we can't unconditionally kill the line. Also, rustfmt will not remove the empty line afterwards.

It'd be possible to add something like suggest_item_without_attr that would do the right thing but it wouldn't be trivial.

@killercup
Copy link
Member

Hm, what does .next_point() on the attribute's span yield? Maybe we can collect the next points while they are char::is_whitespace? Should cover the typical newline and indent as well as attr and fn on one line with space between them.

@etaoins
Copy link
Contributor Author

etaoins commented Jan 17, 2018

@killercup

Something like this:

let mut remove_span = attr.span;
let fmpos = cx.sess().codemap().lookup_byte_offset(remove_span.next_point().hi());

if let Some(ref src) = fmpos.fm.src {
    let non_whitespace_offset = src[fmpos.pos.to_usize()..].find(|c| {
        c != ' ' && c != '\t' && c != '\n'
    });

    if let Some(non_whitespace_offset) = non_whitespace_offset {
        remove_span = remove_span.with_hi(remove_span.hi() + BytePos(non_whitespace_offset as u32))
    }
}

?

Would that be worth pulling it to a helping function and using it for USELESS_ATTRS and INLINE_ALWAYS as well?

@etaoins
Copy link
Contributor Author

etaoins commented Jan 18, 2018

@oli-obk @killercup

I pushed a commit that suggests removing the attribute. Let me know what you think.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 18, 2018

The issue is that the normal diagnostics now also hilight the \n. This is an issue in rustc, but should be easy enough to address.

@etaoins
Copy link
Contributor Author

etaoins commented Jan 18, 2018

@oli-obk
Are you referring to how the entire whitespace suggestion span is being rendered instead of just the attribute? Do you think rustc should unconditionally trim whitespace in suggestions? I think that would break suggesting e.g. reindenting.

Would that be a blocker for merging this PR?

@oli-obk
Copy link
Contributor

oli-obk commented Jan 18, 2018

Do you think rustc should unconditionally trim whitespace in suggestions?

Yes

I think that would break suggesting e.g. reindenting.

No, because it'll only be done in the command line diagnostics, not the json diagnostics

Would that be a blocker for merging this PR?

Nope. We'll see the ui tests breaking when it gets fixed :)

@oli-obk
Copy link
Contributor

oli-obk commented Jan 18, 2018

You need to rebase/merge the current master and update the ui tests again. Something changed with the error reporting

As discussed in rust-lang/rust#47475 the #[inline] attribute is
currently allowed on trait methods without bodies (i.e. without a
default implementation). This is misleading as it could be interpreted
as affecting the implementations of the trait method. Add a lint for any
use of #[inline] on a trait method without a body.

Fixes rust-lang/rust#47475
This adds a `suggest_remove_item` helper that will remove an item and
all trailing whitespace. This should handle both attributes on the same
line as the function and on a separate line; the function takes the
position of the original attribute.
@oli-obk oli-obk merged commit 3c60641 into rust-lang:master Jan 19, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Jan 19, 2018

Thanks!

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.

3 participants