Skip to content

Fix and improve match_type_on_diagnostic_item - #7962

Merged
bors merged 2 commits into
rust-lang:masterfrom
Jarcho:fix_match_type_on_diagnostic_items
Oct 2, 2022
Merged

bors merged 2 commits into
rust-lang:masterfrom
Jarcho:fix_match_type_on_diagnostic_items

Conversation

@Jarcho

@Jarcho Jarcho commented Nov 12, 2021

Copy link
Copy Markdown
Contributor

This extracts the fix for the lint out of #7647. There's still a couple of other functions to check, but at least this will get lint working again.

The two added util functions (is_diagnostic_item and is_lang_item) are needed to handle DefId for unit and tuple struct/variant constructors. The rustc_diagnostic_item and lang attributes are attached to the struct/variant DefId, but most of the time they are used through their constructors which have a different DefId. The two utility functions will check if the DefId is for a constructor and switch to the associated struct/variant DefId.

There does seem to be a bug on rustc's side where constructor DefIds from external crates seem to be returning DefKind::Variant instead of DefKind::Ctor(). There's a workaround put in right.

changelog: None

@rust-highfive

Copy link
Copy Markdown

r? @giraffate

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 12, 2021
@camsteffen

Copy link
Copy Markdown
Contributor

Hmm now there are two functions named is_diagnostic_item with slightly different semantics. Would it work to have is_diagnostic_item_ctor? I would think we should always know whether or not to expect a ctor, basically only with ExprKind::Call.

@Jarcho

Jarcho commented Nov 12, 2021 •

Copy link
Copy Markdown
Contributor Author

I added them so the automated switch from match_def_path would work. Otherwise the lint needs to know which one to use for each case. The ctor DefId will always be used in an expression or pattern, but for a random DefId it could also be from the item directly.

The distinction between the two is also useless in clippy.

They could be named is_*_item_or_ctor which is what's actually happening, but that's also a cumbersome name.

@bors

bors commented Nov 16, 2021

Copy link
Copy Markdown
Contributor

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

@camsteffen

Copy link
Copy Markdown
Contributor

for a random DefId it could also be from the item directly

I don't think we have any cases where generality over the two (ctor or not) is actually needed. We are always expecting one or the other depending on context. And the ctor cases are much less common. So we can put in tcx.parent(ctor_id) for those cases and it shouldn't add up to much.

The distinction between the two is also useless in clippy

That is maybe true, but we still have to deal with the distinction. If we "cover up" the ctor DefIds, making it look like it is the diagnostic item id when it is not, that will lead to surprises when you go to use the DefId with other queries.

@Jarcho

Jarcho commented Nov 18, 2021

Copy link
Copy Markdown
Contributor Author

I don't think we have any cases where generality over the two (ctor or not) is actually needed.

I was referring to the lint suggestion in this case. At the point match_def_path is called it is just a DefId from an unknown source. I guess just suggesting both options wouldn't be a big deal either for an internal lint.

@camsteffen

Copy link
Copy Markdown
Contributor

Ah right. Maybe you could query whether the item has a public constructor to at least narrow down some cases.

@Jarcho

Jarcho commented Nov 18, 2021

Copy link
Copy Markdown
Contributor Author

The lint has to look up the DefId of the item being checked for, so it's easy enough to see if it's a tuple/unit variant. Should I just suggest reworking the code to use is_lang_ctor or is_diagnostic_ctor (and add that one) for those? Otherwise the current functions would need to stay and get renamed.

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch 2 times, most recently from e7682a4 to 2c8c60e Compare January 1, 2022 23:19
@Jarcho

Jarcho commented Jan 1, 2022

Copy link
Copy Markdown
Contributor Author

First attempt at handling ctor suggestions differently.

is_diagnostic_item_or_ctor and is_lang_item_or_ctor are used for structs with publicly accessible constructors as there's no (not incredibly difficult) way to distinguish where a DefId came from.

is_diagnostic_ctor is the counterpart to is_lang_item. There aren't any uses of it, but it's needed to provide a suggestion.

@bors

bors commented Jan 4, 2022

Copy link
Copy Markdown
Contributor

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

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch 2 times, most recently from af141d2 to d588291 Compare February 19, 2022 05:51
@bors

bors commented Feb 24, 2022

Copy link
Copy Markdown
Contributor

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

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch 2 times, most recently from 5e63922 to 296385d Compare May 27, 2022 17:33
@bors

bors commented Jun 27, 2022

Copy link
Copy Markdown
Contributor

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

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch 5 times, most recently from 35380be to 4dd60d1 Compare June 29, 2022 05:54
@Jarcho

Jarcho commented Jun 29, 2022

Copy link
Copy Markdown
Contributor Author

The only extra function introduced now is is_res_diagnostic_ctor which is unused because there aren't any diagnostic item variants currently.

@bors

bors commented Jun 30, 2022

Copy link
Copy Markdown
Contributor

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

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch 2 times, most recently from 6213b20 to 48a8aa3 Compare July 18, 2022 00:14
@bors

bors commented Aug 2, 2022

Copy link
Copy Markdown
Contributor

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

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch 3 times, most recently from c9f3354 to 6b003cc Compare August 20, 2022 06:04
@bors

bors commented Aug 21, 2022

Copy link
Copy Markdown
Contributor

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

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch 3 times, most recently from 1d59045 to 9834da1 Compare October 2, 2022 03:13
@bors

bors commented Oct 2, 2022

Copy link
Copy Markdown
Contributor

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

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch from 9834da1 to 6b961f2 Compare October 2, 2022 13:03
@Jarcho

Jarcho commented Oct 2, 2022

Copy link
Copy Markdown
Contributor Author

r? @llogiq

It looks like @giraffate is either busy or forgot about this.

@rust-highfive rust-highfive assigned llogiq and unassigned giraffate Oct 2, 2022
@llogiq

llogiq commented Oct 2, 2022

Copy link
Copy Markdown
Contributor

Dogfood says no:

2022-10-02T13:08:48.0943209Z     Checking clippy_lints v0.1.66 (/home/runner/work/rust-clippy/rust-clippy/clippy_lints)
2022-10-02T13:08:48.0943818Z error: dereferencing a slice pattern where every element takes a reference
2022-10-02T13:08:48.0944250Z   --> src/methods/manual_ok_or.rs:25:41
2022-10-02T13:08:48.0944504Z    |
2022-10-02T13:08:48.0957818Z 25 |         if let ExprKind::Call(err_path, &[ref err_arg]) = or_expr.kind;
2022-10-02T13:08:48.0958204Z    |                                         ^^^^^^^^^^^^^^
2022-10-02T13:08:48.0958444Z    |
2022-10-02T13:08:48.0959050Z    = note: `-D clippy::needless-borrowed-reference` implied by `-D clippy::all`
2022-10-02T13:08:48.0959790Z    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference
2022-10-02T13:08:48.0960246Z help: try removing the `&` and `ref` parts
2022-10-02T13:08:48.0960518Z    |
2022-10-02T13:08:48.0960949Z 25 -         if let ExprKind::Call(err_path, &[ref err_arg]) = or_expr.kind;
2022-10-02T13:08:48.0961336Z 25 +         if let ExprKind::Call(err_path, [err_arg]) = or_expr.kind;

@Jarcho

Jarcho commented Oct 2, 2022

Copy link
Copy Markdown
Contributor Author

Oops. Looks like I didn't push when I fixed that.

@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch from 6b961f2 to cc86097 Compare October 2, 2022 18:52
* Check for `const`s and `static`s from external crates
* Check for `LangItem`s
* Handle inherent functions which have the same name as a field
* Also check the following functions:
    * `match_trait_method`
    * `match_def_path`
    * `is_expr_path_def_path`
    * `is_qpath_def_path`
* Handle checking for a constructor to a diagnostic item or `LangItem`
@Jarcho
Jarcho force-pushed the fix_match_type_on_diagnostic_items branch from cc86097 to 162aa19 Compare October 2, 2022 19:03
@llogiq

llogiq commented Oct 2, 2022

Copy link
Copy Markdown
Contributor

LGTM.

Thank you, this approach seems a clear improvement.

@bors r+

@bors

bors commented Oct 2, 2022

Copy link
Copy Markdown
Contributor

📌 Commit 162aa19 has been approved by llogiq

It is now in the queue for this repository.

@bors

bors commented Oct 2, 2022

Copy link
Copy Markdown
Contributor

⌛ Testing commit 162aa19 with merge bef93d3...

@bors

bors commented Oct 2, 2022

Copy link
Copy Markdown
Contributor

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing bef93d3 to master...

@bors
bors merged commit bef93d3 into rust-lang:master Oct 2, 2022
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.

6 participants