3

fix FP in lint `[needless_match]` by J-ZhengLi · Pull Request #8549 · rust-lang/...

 2 years ago
source link: https://github.com/rust-lang/rust-clippy/pull/8549
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

New issue

fix FP in lint [needless_match] #8549

Conversation

Copy link

Contributor

@J-ZhengLi J-ZhengLi commented on Mar 16

edited

fixes: #8542
fixes: #8551
fixes: #8595
fixes: #8599


changelog: check for more complex custom type, and ignore type coercion in [needless_match]

Copy link

Collaborator

rust-highfive commented on Mar 16

r? @giraffate

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

rust-highfive

added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label

on Mar 16

Copy link

Contributor

Author

J-ZhengLi commented on Mar 16

r? @llogiq

J-ZhengLi

marked this pull request as draft

last month

J-ZhengLi

marked this pull request as ready for review

last month

Copy link

Contributor

Author

J-ZhengLi commented on Mar 16

I remove the ref x => *x match arm check as it's logic seems problematic, I was already unsure about whether I should or not including it when I add the lint. Right now, removing it does seem to be a quick fix of what @teor2345 mentioned in #8542 (comment) , I just don't see the need to add it back, but maybe I'll do it when someone actually suggesting it. facepalm

teor2345 reacted with thumbs up emoji

J-ZhengLi

changed the title quick fix of issue#8542: FP in lint [needless_match]

[WIP] quick fix of issue#8542: FP in lint [needless_match]

on Mar 17

J-ZhengLi

changed the title [WIP] quick fix of issue#8542: FP in lint [needless_match]

quick fix of issue#8542: FP in lint [needless_match]

on Mar 17

/// Check if the type of the match operand differs with the assigned local or function return type

///

/// Can also be used to check for the `let_expr` in `IfLet` as well.

fn is_type_differ(cx: &LateContext<'_>, match_expr: &Expr<'_>, expr: &Expr<'_>) -> bool {

if let Some(p_node) = get_parent_node(cx.tcx, expr.hir_id) {

Copy link

Contributor

Author

@J-ZhengLi J-ZhengLi on Mar 17

edited

I'm not so sure about if this is the correct way to check for type coercion, I basically just check for the assigned variable/function return type to see if those are the same as match expression.

Although I saw methods like TypeckResult::is_coercion_cast or something, but I don't think those work in these case.

Copy link

Contributor

@llogiq llogiq 19 days ago

We have clippy_utils::is_adjusted(cx, expr), which should do the trick more easily.

Copy link

Contributor

Author

@J-ZhengLi J-ZhengLi 17 days ago

We have clippy_utils::is_adjusted(cx, expr), which should do the trick more easily.

Well... I later realized that I might still need to keep it this way, because I do need to make sure that the type of match input and output are the same, not just coercion but also as what #8599 suggested. unless there is another util function that I can use to do the check.

llogiq reacted with thumbs up emoji

J-ZhengLi

changed the title quick fix of issue#8542: FP in lint [needless_match]

[WIP] quick fix of issue#8542: FP in lint [needless_match]

on Mar 17

J-ZhengLi

changed the title [WIP] quick fix of issue#8542: FP in lint [needless_match]

fix FP in lint [needless_match]

on Mar 17

Copy link

Contributor

Author

J-ZhengLi commented 19 days ago

edited

Also fixes #8595, #8599

It's kinda harsh seeing my mistakes being addressed multiple times tbh sob , so if anyone is not busy at the moment (sorry to bother if you are), please help me reviewing this, thanks~~~

edit: Oh... here are some 'ol little pings @giraffate @llogiq

Copy link

Contributor

@llogiq llogiq left a comment

This looks good, I just have a very small suggestion. r=me when applied.

@@ -171,27 +191,30 @@ fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {

false

}

fn has_identical_segments(left_segs: &[PathSegment<'_>], right_segs: &[PathSegment<'_>]) -> bool {

fn same_segments(left_segs: &[PathSegment<'_>], right_segs: &[PathSegment<'_>]) -> bool {

Copy link

Contributor

@llogiq llogiq 15 days ago

I think we have an over function for such things skmewhere in clippy_utils.

Copy link

Contributor

llogiq commented 11 days ago

Thank you!

@bors r+

J-ZhengLi reacted with heart emoji

Copy link

Contributor

bors commented 11 days ago

pushpin Commit 85b081b has been approved by llogiq

Copy link

Contributor

bors commented 11 days ago

hourglass Testing commit 85b081b with merge 81e004a...

bors

merged commit 81e004a into

rust-lang:master 11 days ago

5 checks passed

J-ZhengLi

deleted the issue8542 branch

7 days ago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK