-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,59 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_context; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{ExprKind, Stmt, StmtKind}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for calls to `Result::ok()` without using the returned `Option`. | ||
/// | ||
/// ### Why is this bad? | ||
/// Using `Result::ok()` may look like the result is checked like `unwrap` or `expect` would do | ||
/// but it only silences the warning caused by `#[must_use]` on the `Result`. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// # fn some_function() -> Result<(), ()> { Ok(()) } | ||
/// some_function().ok(); | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// # fn some_function() -> Result<(), ()> { Ok(()) } | ||
/// let _ = some_function(); | ||
/// ``` | ||
#[clippy::version = "1.70.0"] | ||
pub UNUSED_RESULT_OK, | ||
restriction, | ||
"Use of `.ok()` to silence `Result`'s `#[must_use]` is misleading. Use `let _ =` instead." | ||
} | ||
declare_lint_pass!(UnusedResultOk => [UNUSED_RESULT_OK]); | ||
|
||
impl LateLintPass<'_> for UnusedResultOk { | ||
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { | ||
if let StmtKind::Semi(expr) = stmt.kind | ||
&& let ExprKind::MethodCall(ok_path, recv, [], ..) = expr.kind //check is expr.ok() has type Result<T,E>.ok(, _) | ||
&& ok_path.ident.as_str() == "ok" | ||
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result) | ||
&& !in_external_macro(cx.sess(), stmt.span) | ||
{ | ||
let ctxt = expr.span.ctxt(); | ||
let mut applicability = Applicability::MaybeIncorrect; | ||
let snippet = snippet_with_context(cx, recv.span, ctxt, "", &mut applicability).0; | ||
let sugg = format!("let _ = {snippet}"); | ||
span_lint_and_sugg( | ||
cx, | ||
UNUSED_RESULT_OK, | ||
expr.span, | ||
"ignoring a result with `.ok()` is misleading", | ||
"consider using `let _ =` and removing the call to `.ok()` instead", | ||
sugg, | ||
applicability, | ||
); | ||
} | ||
} | ||
} |
This file contains 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,40 @@ | ||
//@aux-build:proc_macros.rs | ||
#![warn(clippy::unused_result_ok)] | ||
#![allow(dead_code)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
fn bad_style(x: &str) { | ||
let _ = x.parse::<u32>(); | ||
} | ||
|
||
fn good_style(x: &str) -> Option<u32> { | ||
x.parse::<u32>().ok() | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn strange_parse(x: &str) { | ||
let _ = x . parse::<i32>(); | ||
} | ||
|
||
macro_rules! v { | ||
() => { | ||
Ok::<(), ()>(()) | ||
}; | ||
} | ||
|
||
macro_rules! w { | ||
() => { | ||
let _ = Ok::<(), ()>(()); | ||
}; | ||
} | ||
|
||
fn main() { | ||
let _ = v!(); | ||
w!(); | ||
|
||
external! { | ||
Ok::<(),()>(()).ok(); | ||
}; | ||
} |
This file contains 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,40 @@ | ||
//@aux-build:proc_macros.rs | ||
#![warn(clippy::unused_result_ok)] | ||
#![allow(dead_code)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
fn bad_style(x: &str) { | ||
x.parse::<u32>().ok(); | ||
} | ||
|
||
fn good_style(x: &str) -> Option<u32> { | ||
x.parse::<u32>().ok() | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn strange_parse(x: &str) { | ||
x . parse::<i32>() . ok (); | ||
} | ||
|
||
macro_rules! v { | ||
() => { | ||
Ok::<(), ()>(()) | ||
}; | ||
} | ||
|
||
macro_rules! w { | ||
() => { | ||
Ok::<(), ()>(()).ok(); | ||
}; | ||
} | ||
|
||
fn main() { | ||
v!().ok(); | ||
w!(); | ||
|
||
external! { | ||
Ok::<(),()>(()).ok(); | ||
}; | ||
} |
This file contains 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,52 @@ | ||
error: ignoring a result with `.ok()` is misleading | ||
--> tests/ui/unused_result_ok.rs:9:5 | ||
| | ||
LL | x.parse::<u32>().ok(); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::unused-result-ok` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unused_result_ok)]` | ||
help: consider using `let _ =` and removing the call to `.ok()` instead | ||
| | ||
LL | let _ = x.parse::<u32>(); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: ignoring a result with `.ok()` is misleading | ||
--> tests/ui/unused_result_ok.rs:18:5 | ||
| | ||
LL | x . parse::<i32>() . ok (); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider using `let _ =` and removing the call to `.ok()` instead | ||
| | ||
LL | let _ = x . parse::<i32>(); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: ignoring a result with `.ok()` is misleading | ||
--> tests/ui/unused_result_ok.rs:34:5 | ||
| | ||
LL | v!().ok(); | ||
| ^^^^^^^^^ | ||
| | ||
help: consider using `let _ =` and removing the call to `.ok()` instead | ||
| | ||
LL | let _ = v!(); | ||
| ~~~~~~~~~~~~ | ||
|
||
error: ignoring a result with `.ok()` is misleading | ||
--> tests/ui/unused_result_ok.rs:29:9 | ||
| | ||
LL | Ok::<(), ()>(()).ok(); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
... | ||
LL | w!(); | ||
| ---- in this macro invocation | ||
| | ||
= note: this error originates in the macro `w` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
help: consider using `let _ =` and removing the call to `.ok()` instead | ||
| | ||
LL | let _ = Ok::<(), ()>(()); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 4 previous errors | ||
|