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

[unnecessary_unwrap]: lint on .as_ref().unwrap() #11387

Merged
merged 2 commits into from Aug 28, 2023
Merged

Conversation

y21
Copy link
Member

@y21 y21 commented Aug 23, 2023

Closes #11371

This turned out to be a little more code than I originally thought, because the lint also makes sure to not lint if the user tries to mutate the option:

if option.is_some() {
  option = None;
  option.unwrap(); // don't lint here
}

... which means that even if we taught this lint to recognize .as_mut(), it would still not lint because that would count as a mutation. So we need to allow .as_mut() calls but reject other kinds of mutations.
Unfortunately it doesn't look like this is possible with is_potentially_mutated (seeing what kind of mutation happened).
This replaces it with a custom little visitor that does basically what it did before, but also allows .as_mut().

changelog: [unnecessary_unwrap]: lint on .as_ref().unwrap()

@rustbot
Copy link
Collaborator

rustbot commented Aug 23, 2023

r? @blyxyas

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 23, 2023
@blyxyas
Copy link
Member

blyxyas commented Aug 23, 2023

This turned out to be a little more code than I originally thought, because the lint also makes sure to not lint if the user tries to mutate the option

Wouldn't that also be... an unnecessary unwrap?

@y21
Copy link
Member Author

y21 commented Aug 23, 2023

Oh true 😄 that example might've been confusing... I suppose

if option.is_some() {
  option = calculate_option(); // some kind of expression that mutates option, we don't really know what this does
  option.unwrap();
}

would be more realistic. Suggesting to change that to an if let wouldnt preserve semantics since that mutation may change it to None.
But as_mut specifically is ok to allow, since that cannot change it to None.

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Wow, it really is a bit of code, but it seems really good. Just a couple questions 🐱

clippy_lints/src/unwrap.rs Show resolved Hide resolved
clippy_utils/src/usage.rs Show resolved Hide resolved
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

I think this is pretty good to go then, thanks! ❤️

@blyxyas
Copy link
Member

blyxyas commented Aug 28, 2023

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 28, 2023

📌 Commit f80c55d has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Aug 28, 2023

⌛ Testing commit f80c55d with merge ec17cd8...

bors added a commit that referenced this pull request Aug 28, 2023
[`unnecessary_unwrap`]: lint on `.as_ref().unwrap()`

Closes #11371

This turned out to be a little more code than I originally thought, because the lint also makes sure to not lint if the user tries to mutate the option:
```rs
if option.is_some() {
  option = None;
  option.unwrap(); // don't lint here
}
```
... which means that even if we taught this lint to recognize `.as_mut()`, it would *still* not lint because that would count as a mutation. So we need to allow `.as_mut()` calls but reject other kinds of mutations.
Unfortunately it doesn't look like this is possible with `is_potentially_mutated` (seeing what kind of mutation happened).
This replaces it with a custom little visitor that does basically what it did before, but also allows `.as_mut()`.

changelog: [`unnecessary_unwrap`]: lint on `.as_ref().unwrap()`
@bors
Copy link
Collaborator

bors commented Aug 28, 2023

💔 Test failed - checks-action_test

@y21
Copy link
Member Author

y21 commented Aug 28, 2023

Looks like a spurious error?

@blyxyas
Copy link
Member

blyxyas commented Aug 28, 2023

Looks like one :3
@bors r+

@bors
Copy link
Collaborator

bors commented Aug 28, 2023

💡 This pull request was already approved, no need to approve it again.

  • This pull request previously failed. You should add more commits to fix the bug, or use retry to trigger a build again.

@bors
Copy link
Collaborator

bors commented Aug 28, 2023

📌 Commit f80c55d has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Aug 28, 2023

⌛ Testing commit f80c55d with merge b97eaab...

@bors
Copy link
Collaborator

bors commented Aug 28, 2023

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

@bors bors merged commit b97eaab into rust-lang:master Aug 28, 2023
5 checks passed
linyihai pushed a commit to linyihai/rust-clippy that referenced this pull request Oct 23, 2023
Auto merge of rust-lang#11387 - y21:issue11371, r=blyxyas

Created-by: chenshuo 00511327
Author-id: 1557
MR-id: 575518
Commit-by: chenshuo c00511327;bors;mojave2;Samuel Moelius;y21;Alex Macleod;Meng Huang;Philipp Krones;xFrednet;Caio;lengyijun;Red Rapious;Igor Aleksanov;Esteban Küber;Guillaume Gomez;Ralf Jung;Ethan Brierley;blyxyas;Matthias Krüger;J-ZhengLi;Nilstrieb;Deadbeef;unvalley;Samuel Tardieu;Oli Scherer;Max Niederman;Catherine Flores;Timo;Vadim Petrochenkov;León Orell Valerian Liehr;Manish Goregaokar;ouz-a;Morten Lohne;Your Name;yukang;Urgau;Samuel "Sam" Tardieu;Maybe Waffle;Jason Newcomb;Trevor Gross;Catherine;Michael Goulet;David Wood;Milo Moisson;Centri3;Thibaut Vandervelden;lcnr;Sylvestre Ledru;Mahdi Dibaiee;syvb;Mingwei Samuel;Panagiotis Foliadis;Eric Huss;Boxy;est31;darklyspaced;许杰友 Jieyou Xu (Joe);Renato Lochetti;Eric Mark Martin;hehaoqian;dswij;Martin Fischer;Kisaragi;Kisaragi Marine;Pavan Kumar Sunkara;rsdy;avborhanian;Andrew Xie;asquared31415;MarcusGrass;Alessio Cosenza;Brian Hetro;Raghul Nanth A;ihciah;beetrees;disco07;Charalampos Mitrodimas;Fridtjof Stoldt;Nicholas Nethercote;Kyle Matsuda;klensy;Takayuki Nakata;Yuri Astrakhan;adrianEffe
Merged-by: lizheng 30020856
E2E-issues: issue16
Description: Auto merge of rust-lang#11387 - y21:issue11371, r=blyxyas

[`unnecessary_unwrap`]: lint on `.as_ref().unwrap()`

Closes rust-lang#11371

This turned out to be a little more code than I originally thought, because the lint also makes sure to not lint if the user tries to mutate the option:
```rs
if option.is_some() {
  option = None;
  option.unwrap(); // don't lint here
}
```
... which means that even if we taught this lint to recognize `.as_mut()`, it would *still* not lint because that would count as a mutation. So we need to allow `.as_mut()` calls but reject other kinds of mutations.
Unfortunately it doesn't look like this is possible with `is_potentially_mutated` (seeing what kind of mutation happened).
This replaces it with a custom little visitor that does basically what it did before, but also allows `.as_mut()`.

changelog: [`unnecessary_unwrap`]: lint on `.as_ref().unwrap()`

[rust-lang#16 同步社区master最新代码](https://open.codehub.huawei.com/innersource/rust/toolset/rust-clippy/issues/16) 

See merge request: innersource/rust/toolset/rust-clippy!18
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.

unnecessary_unwrap doesn't work with .as_ref().unwrap() and .as_mut().unwrap()
4 participants