-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add map unwrap or lint for Result #4822
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9c4c360
lint: add map(f).unwrap_or(a) for Result
11f5d3d
add unit test for result_map_unwrap_or
b55ead5
add documentation
0d4c7ec
add a debug_assert according to the comment
2ecaf8f
fix code comments
756e78f
use another name 'lint_type' to avoid the same name as the function
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,19 @@ use rustc::lint::LateContext; | |
use rustc_data_structures::fx::FxHashSet; | ||
use syntax_pos::symbol::Symbol; | ||
|
||
use super::OPTION_MAP_UNWRAP_OR; | ||
use super::{OPTION_MAP_UNWRAP_OR, RESULT_MAP_UNWRAP_OR}; | ||
|
||
/// lint use of `map().unwrap_or()` for `Option`s | ||
/// lint use of `map().unwrap_or()` for `Option`s and `Result`s | ||
pub(super) fn lint<'a, 'tcx>( | ||
cx: &LateContext<'a, 'tcx>, | ||
expr: &hir::Expr, | ||
map_args: &'tcx [hir::Expr], | ||
unwrap_args: &'tcx [hir::Expr], | ||
) { | ||
// lint if the caller of `map()` is an `Option` | ||
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) { | ||
let is_option = match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION); | ||
let is_result = match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::RESULT); | ||
|
||
if is_option || is_result { | ||
if !is_copy(cx, cx.tables.expr_ty(&unwrap_args[1])) { | ||
// Do not lint if the `map` argument uses identifiers in the `map` | ||
// argument that are also used in the `unwrap_or` argument | ||
|
@@ -43,19 +45,33 @@ pub(super) fn lint<'a, 'tcx>( | |
let map_snippet = snippet(cx, map_args[1].span, ".."); | ||
let unwrap_snippet = snippet(cx, unwrap_args[1].span, ".."); | ||
// lint message | ||
// comparing the snippet from source to raw text ("None") below is safe | ||
// because we already have checked the type. | ||
let arg = if unwrap_snippet == "None" { "None" } else { "a" }; | ||
let suggest = if unwrap_snippet == "None" { | ||
"and_then(f)" | ||
let msg = if is_option { | ||
// comparing the snippet from source to raw text ("None") below is safe | ||
// because we already have checked the type. | ||
let (arg, suggest) = if unwrap_snippet == "None" { | ||
("None", "and_then(f)") | ||
} else { | ||
("a", "map_or(a, f)") | ||
}; | ||
|
||
format!( | ||
"called `map(f).unwrap_or({})` on an Option value. \ | ||
This can be done more directly by calling `{}` instead", | ||
arg, suggest | ||
) | ||
} else { | ||
"map_or(a, f)" | ||
debug_assert!(is_result); | ||
"called `map(f).unwrap_or(a)` on a Result value. \ | ||
This can be done more directly by calling `map_or(a, f)` instead" | ||
Comment on lines
+64
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you do this by using the function span_lint_and_then(
..,
|db| {
db.note("this can be done more directly by calling `map_or(a, f)` instead");
if multiline {
db.span_help(.., format!("replace `map({}).unwrap_or({})` with `{}`", ..);
}
},
); |
||
.to_string() | ||
}; | ||
let msg = &format!( | ||
"called `map(f).unwrap_or({})` on an Option value. \ | ||
This can be done more directly by calling `{}` instead", | ||
arg, suggest | ||
); | ||
|
||
let lint_type = if is_option { | ||
OPTION_MAP_UNWRAP_OR | ||
} else { | ||
RESULT_MAP_UNWRAP_OR | ||
}; | ||
|
||
// lint, with note if neither arg is > 1 line and both map() and | ||
// unwrap_or() have the same span | ||
let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1; | ||
|
@@ -70,9 +86,9 @@ pub(super) fn lint<'a, 'tcx>( | |
"replace `map({}).unwrap_or({})` with `{}`", | ||
map_snippet, unwrap_snippet, suggest | ||
); | ||
span_note_and_lint(cx, OPTION_MAP_UNWRAP_OR, expr.span, msg, expr.span, ¬e); | ||
span_note_and_lint(cx, lint_type, expr.span, &msg, expr.span, ¬e); | ||
} else if same_span && multiline { | ||
span_lint(cx, OPTION_MAP_UNWRAP_OR, expr.span, msg); | ||
span_lint(cx, lint_type, expr.span, &msg); | ||
}; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![warn(clippy::result_map_unwrap_or)] | ||
|
||
fn main() { | ||
let res: Result<usize, ()> = Ok(1); | ||
|
||
// Check `RESULT_MAP_OR_NONE`. | ||
// Single line case. | ||
let _ = res.map(|x| x + 1).unwrap_or(0); | ||
// Multi-line case. | ||
#[rustfmt::skip] | ||
let _ = res.map(|x| { | ||
x + 1 | ||
}).unwrap_or(0); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: called `map(f).unwrap_or(a)` on a Result value. This can be done more directly by calling `map_or(a, f)` instead | ||
--> $DIR/result_map_unwrap_or.rs:8:13 | ||
| | ||
LL | let _ = res.map(|x| x + 1).unwrap_or(0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::result-map-unwrap-or` implied by `-D warnings` | ||
= note: replace `map(|x| x + 1).unwrap_or(0)` with `map_or(0, |x| x + 1)` | ||
|
||
error: called `map(f).unwrap_or(a)` on a Result value. This can be done more directly by calling `map_or(a, f)` instead | ||
--> $DIR/result_map_unwrap_or.rs:11:13 | ||
| | ||
LL | let _ = res.map(|x| { | ||
| _____________^ | ||
LL | | x + 1 | ||
LL | | }).unwrap_or(0); | ||
| |______________________________________^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.