diff --git a/CHANGELOG.md b/CHANGELOG.md index c838fa2f4961..5cabb5b072cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5347,7 +5347,6 @@ Released 2018-09-13 [`min_ident_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_ident_chars [`min_max`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_max [`misaligned_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#misaligned_transmute -[`misleading_use_of_ok`]: https://rust-lang.github.io/rust-clippy/master/index.html#misleading_use_of_ok [`mismatched_target_os`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatched_target_os [`mismatching_type_param_order`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatching_type_param_order [`misnamed_getters`]: https://rust-lang.github.io/rust-clippy/master/index.html#misnamed_getters @@ -5704,6 +5703,7 @@ Released 2018-09-13 [`unused_io_amount`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_io_amount [`unused_label`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_label [`unused_peekable`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_peekable +[`unused_result_ok`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_result_ok [`unused_rounding`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_rounding [`unused_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_self [`unused_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index d1cb22ab8905..291a24c27811 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -477,7 +477,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::misc_early::UNNEEDED_WILDCARD_PATTERN_INFO, crate::misc_early::UNSEPARATED_LITERAL_SUFFIX_INFO, crate::misc_early::ZERO_PREFIXED_LITERAL_INFO, - crate::misleading_use_of_ok::MISLEADING_USE_OF_OK_INFO, crate::mismatching_type_param_order::MISMATCHING_TYPE_PARAM_ORDER_INFO, crate::missing_assert_message::MISSING_ASSERT_MESSAGE_INFO, crate::missing_asserts_for_indexing::MISSING_ASSERTS_FOR_INDEXING_INFO, @@ -711,6 +710,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::unused_async::UNUSED_ASYNC_INFO, crate::unused_io_amount::UNUSED_IO_AMOUNT_INFO, crate::unused_peekable::UNUSED_PEEKABLE_INFO, + crate::unused_result_ok::UNUSED_RESULT_OK_INFO, crate::unused_rounding::UNUSED_ROUNDING_INFO, crate::unused_self::UNUSED_SELF_INFO, crate::unused_unit::UNUSED_UNIT_INFO, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 7b8db0cec0a0..084ca2873ff8 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -212,7 +212,6 @@ mod min_ident_chars; mod minmax; mod misc; mod misc_early; -mod misleading_use_of_ok; mod mismatching_type_param_order; mod missing_assert_message; mod missing_asserts_for_indexing; @@ -350,6 +349,7 @@ mod unsafe_removed_from_name; mod unused_async; mod unused_io_amount; mod unused_peekable; +mod unused_result_ok; mod unused_rounding; mod unused_self; mod unused_unit; @@ -766,7 +766,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) { store.register_late_pass(move |_| Box::new(missing_doc::MissingDoc::new(missing_docs_in_crate_items))); store.register_late_pass(|_| Box::new(missing_inline::MissingInline)); store.register_late_pass(move |_| Box::new(exhaustive_items::ExhaustiveItems)); - store.register_late_pass(|_| Box::new(misleading_use_of_ok::MisleadingUseOfOk)); + store.register_late_pass(|_| Box::new(unused_result_ok::UnusedResultOk)); store.register_late_pass(|_| Box::new(match_result_ok::MatchResultOk)); store.register_late_pass(|_| Box::new(partialeq_ne_impl::PartialEqNeImpl)); store.register_late_pass(|_| Box::new(unused_io_amount::UnusedIoAmount)); diff --git a/clippy_lints/src/misleading_use_of_ok.rs b/clippy_lints/src/unused_result_ok.rs similarity index 92% rename from clippy_lints/src/misleading_use_of_ok.rs rename to clippy_lints/src/unused_result_ok.rs index bf4f48cf4766..35f07eba9b07 100644 --- a/clippy_lints/src/misleading_use_of_ok.rs +++ b/clippy_lints/src/unused_result_ok.rs @@ -27,13 +27,13 @@ declare_clippy_lint! { /// let _ = some_function(); /// ``` #[clippy::version = "1.70.0"] - pub MISLEADING_USE_OF_OK, + pub UNUSED_RESULT_OK, style, "Use of `.ok()` to silence `Result`'s `#[must_use]` is misleading. Use `let _ =` instead." } -declare_lint_pass!(MisleadingUseOfOk => [MISLEADING_USE_OF_OK]); +declare_lint_pass!(UnusedResultOk => [UNUSED_RESULT_OK]); -impl LateLintPass<'_> for MisleadingUseOfOk { +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.ok(, _) @@ -47,7 +47,7 @@ impl LateLintPass<'_> for MisleadingUseOfOk { let sugg = format!("let _ = {snippet}"); span_lint_and_sugg( cx, - MISLEADING_USE_OF_OK, + UNUSED_RESULT_OK, expr.span, "ignoring a result with `.ok()` is misleading", "consider using `let _ =` and removing the call to `.ok()` instead", diff --git a/tests/ui/misleading_use_of_ok.fixed b/tests/ui/misleading_use_of_ok.fixed deleted file mode 100644 index 9043370287a1..000000000000 --- a/tests/ui/misleading_use_of_ok.fixed +++ /dev/null @@ -1,17 +0,0 @@ -#![warn(clippy::misleading_use_of_ok)] -#![allow(dead_code)] - -fn bad_style(x: &str) { - let _ = x.parse::(); -} - -fn good_style(x: &str) -> Option { - x.parse::().ok() -} - -#[rustfmt::skip] -fn strange_parse(x: &str) { - let _ = x . parse::(); -} - -fn main() {} diff --git a/tests/ui/misleading_use_of_ok.rs b/tests/ui/misleading_use_of_ok.rs deleted file mode 100644 index 26da26aaca52..000000000000 --- a/tests/ui/misleading_use_of_ok.rs +++ /dev/null @@ -1,17 +0,0 @@ -#![warn(clippy::misleading_use_of_ok)] -#![allow(dead_code)] - -fn bad_style(x: &str) { - x.parse::().ok(); -} - -fn good_style(x: &str) -> Option { - x.parse::().ok() -} - -#[rustfmt::skip] -fn strange_parse(x: &str) { - x . parse::() . ok (); -} - -fn main() {} diff --git a/tests/ui/misleading_use_of_ok.stderr b/tests/ui/misleading_use_of_ok.stderr deleted file mode 100644 index 3b1219042500..000000000000 --- a/tests/ui/misleading_use_of_ok.stderr +++ /dev/null @@ -1,26 +0,0 @@ -error: ignoring a result with `.ok()` is misleading - --> $DIR/misleading_use_of_ok.rs:5:5 - | -LL | x.parse::().ok(); - | ^^^^^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::misleading-use-of-ok` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::misleading_use_of_ok)]` -help: consider using `let _ =` and removing the call to `.ok()` instead - | -LL | let _ = x.parse::(); - | ~~~~~~~~~~~~~~~~~~~~~~~~ - -error: ignoring a result with `.ok()` is misleading - --> $DIR/misleading_use_of_ok.rs:14:5 - | -LL | x . parse::() . ok (); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: consider using `let _ =` and removing the call to `.ok()` instead - | -LL | let _ = x . parse::(); - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -error: aborting due to 2 previous errors - diff --git a/tests/ui/unused_result_ok.fixed b/tests/ui/unused_result_ok.fixed new file mode 100644 index 000000000000..e78fde5c9e3c --- /dev/null +++ b/tests/ui/unused_result_ok.fixed @@ -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::(); +} + +fn good_style(x: &str) -> Option { + x.parse::().ok() +} + +#[rustfmt::skip] +fn strange_parse(x: &str) { + let _ = x . parse::(); +} + +macro_rules! v { + () => { + Ok::<(), ()>(()) + }; +} + +macro_rules! w { + () => { + let _ = Ok::<(), ()>(()); + }; +} + +fn main() { + let _ = v!(); + w!(); + + external! { + Ok::<(),()>(()).ok(); + }; +} diff --git a/tests/ui/unused_result_ok.rs b/tests/ui/unused_result_ok.rs new file mode 100644 index 000000000000..117d64c4cec6 --- /dev/null +++ b/tests/ui/unused_result_ok.rs @@ -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::().ok(); +} + +fn good_style(x: &str) -> Option { + x.parse::().ok() +} + +#[rustfmt::skip] +fn strange_parse(x: &str) { + x . parse::() . ok (); +} + +macro_rules! v { + () => { + Ok::<(), ()>(()) + }; +} + +macro_rules! w { + () => { + Ok::<(), ()>(()).ok(); + }; +} + +fn main() { + v!().ok(); + w!(); + + external! { + Ok::<(),()>(()).ok(); + }; +} diff --git a/tests/ui/unused_result_ok.stderr b/tests/ui/unused_result_ok.stderr new file mode 100644 index 000000000000..bfeafaf63bad --- /dev/null +++ b/tests/ui/unused_result_ok.stderr @@ -0,0 +1,52 @@ +error: ignoring a result with `.ok()` is misleading + --> $DIR/unused_result_ok.rs:9:5 + | +LL | x.parse::().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::(); + | ~~~~~~~~~~~~~~~~~~~~~~~~ + +error: ignoring a result with `.ok()` is misleading + --> $DIR/unused_result_ok.rs:18:5 + | +LL | x . parse::() . ok (); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: consider using `let _ =` and removing the call to `.ok()` instead + | +LL | let _ = x . parse::(); + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +error: ignoring a result with `.ok()` is misleading + --> $DIR/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 + --> $DIR/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 +