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

[useless_conversion]: only lint on paths to fn items and fix FP in macro #11070

Merged
merged 3 commits into from
Aug 17, 2023

Conversation

y21
Copy link
Member

@y21 y21 commented Jul 2, 2023

Fixes #11065 (which is actually two issues: an ICE and a false positive)

It now makes sure that the function call path points to a function-like item (and not e.g. a const like in the linked issue), so that calling TyCtxt::fn_sig later in the lint does not ICE (fixes #11065 (comment)).
It also makes sure that the expression is not part of a macro call (fixes #11065 (comment)). I'm not sure if there's a better way to check this other than to walk the parent expr chain and see if any of them are expansions. (edit: it doesn't do this anymore)

changelog: [useless_conversion]: fix ICE when call receiver is a non-fn item
changelog: [useless_conversion]: don't lint if argument is a macro argument (fixes a FP)

r? @llogiq (reviewed #10814, which introduced these issues)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 2, 2023
Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

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

Looks good in general. I only have one question regarding simplicity, otherwise I consider this merge-worthy.

/// Checks if the given `expr` is an argument of a macro invocation.
/// This is a slow-ish operation, so consider calling this late
/// to avoid slowing down the lint in the happy path when not emitting a warning
fn is_macro_argument(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we avoid that by keeping an Option<Span> in our lint to be set when we encounter an expression from a macro? That way this check would become a simple .is_some().

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure. How/When does it get reset to None? If we just set it to Some(span) once we see an expression from an expansion, it would forever think we are in a macro, no? Or do you mean we should also then check if the span in the lint struct fully encloses the span currently being looked at, or something like that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we can have check_expr set the span to Some(span) and then remove it if it matches in check_expr_post.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, but wouldn't this at least also need a stack of spans, to handle nested macro expansions? Alternatively, could we just use a u32 that we increment/decrement for entering or leaving a span from an expansion and check if == 0 when about to lint?

@bors
Copy link
Contributor

bors commented Aug 11, 2023

☔ The latest upstream changes (presumably #11239) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

This LGTM. @llogiq can I ask you to give this another review, please? The issue for this was nominated to be backported, which will happen on Thursday.

@flip1995 flip1995 added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Aug 14, 2023
@y21
Copy link
Member Author

y21 commented Aug 14, 2023

(I didn't change anything with this rebase, just fixed the conflicts)

@flip1995
Copy link
Member

Thanks! I'm going ahead and approve this, so I can include it in the backports.

@bors r+

@bors
Copy link
Contributor

bors commented Aug 17, 2023

📌 Commit f47165c has been approved by flip1995

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 17, 2023

⌛ Testing commit f47165c with merge 701e77c...

@bors
Copy link
Contributor

bors commented Aug 17, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 701e77c to master...

@bors bors merged commit 701e77c into rust-lang:master Aug 17, 2023
@flip1995 flip1995 added beta-accepted Accepted for backporting to the compiler in the beta channel. and removed beta-nominated Nominated for backporting to the compiler in the beta channel. labels Aug 17, 2023
@flip1995
Copy link
Member

flip1995 commented Aug 17, 2023

rust-lang/rust#114937 for beta

and rust-lang/rust#114938 for master

bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 17, 2023
…k-Simulacrum

[beta] Clippy backports for ICE fixes

This backports PRs to beta, that fix ICEs, some lint grouping and FP fixes. Namely:

- rust-lang/rust-clippy#11191
- rust-lang/rust-clippy#11172
- rust-lang/rust-clippy#11130
- rust-lang/rust-clippy#11106
- rust-lang/rust-clippy#11104
- rust-lang/rust-clippy#11077
- rust-lang/rust-clippy#11070 (This PR is not synced to the Rust repo yet, but I will open a separate PR to get it into `master`, before beta is branched: rust-lang#114938)
- rust-lang/rust-clippy#11069

Kind of a big backport, but most of it is tests.

r? `@Mark-Simulacrum`

cc `@Manishearth`
@xFrednet xFrednet removed the beta-accepted Accepted for backporting to the compiler in the beta channel. label Aug 18, 2023
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Aug 24, 2023
[`useless_conversion`]: only lint on paths to fn items and fix FP in macro

Fixes rust-lang#11065 (which is actually two issues: an ICE and a false positive)

It now makes sure that the function call path points to a function-like item (and not e.g. a `const` like in the linked issue), so that calling `TyCtxt::fn_sig` later in the lint does not ICE (fixes rust-lang#11065 (comment)).
It *also* makes sure that the expression is not part of a macro call (fixes rust-lang#11065 (comment)). ~~I'm not sure if there's a better way to check this other than to walk the parent expr chain and see if any of them are expansions.~~ (edit: it doesn't do this anymore)

changelog: [`useless_conversion`]: fix ICE when call receiver is a non-fn item
changelog: [`useless_conversion`]: don't lint if argument is a macro argument (fixes a FP)

r? `@llogiq` (reviewed rust-lang#10814, which introduced these issues)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ice unexpected sort of node in fn_sig(): Item(Item..`
6 participants