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
Conversation
r? @blyxyas (rustbot has picked a reviewer for you, use r? to override) |
Wouldn't that also be... an unnecessary unwrap? |
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 |
There was a problem hiding this 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 🐱
There was a problem hiding this 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! ❤️
@bors r+ |
[`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()`
💔 Test failed - checks-action_test |
Looks like a spurious error? |
Looks like one :3 |
💡 This pull request was already approved, no need to approve it again.
|
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
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
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:
... 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()