Skip to content

Commit

Permalink
Rename [misleading_use_of_ok] to [unused_result_ok] and add macro tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ithinuel committed Jan 28, 2024
1 parent 256291b commit beee858
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 68 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T,E>.ok(, _)
Expand All @@ -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",
Expand Down
17 changes: 0 additions & 17 deletions tests/ui/misleading_use_of_ok.fixed

This file was deleted.

17 changes: 0 additions & 17 deletions tests/ui/misleading_use_of_ok.rs

This file was deleted.

26 changes: 0 additions & 26 deletions tests/ui/misleading_use_of_ok.stderr

This file was deleted.

51 changes: 51 additions & 0 deletions tests/ui/unused_result_ok.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//@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! {
macro_rules! x {
() => { Ok::<(), ()>(()) }
}
macro_rules! y {
() => { Ok::<(), ()>(()).ok(); }
}

x!().ok();
y!();
};

let _ = x!();
y!();
}
51 changes: 51 additions & 0 deletions tests/ui/unused_result_ok.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//@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! {
macro_rules! x {
() => { Ok::<(), ()>(()) }
}
macro_rules! y {
() => { Ok::<(), ()>(()).ok(); }
}

x!().ok();
y!();
};

x!().ok();
y!();
}
63 changes: 63 additions & 0 deletions tests/ui/unused_result_ok.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
error: ignoring a result with `.ok()` is misleading
--> $DIR/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
--> $DIR/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
--> $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: ignoring a result with `.ok()` is misleading
--> $DIR/unused_result_ok.rs:49:5
|
LL | x!().ok();
| ^^^^^^^^^
|
help: consider using `let _ =` and removing the call to `.ok()` instead
|
LL | let _ = x!();
| ~~~~~~~~~~~~

error: aborting due to 5 previous errors

0 comments on commit beee858

Please sign in to comment.