Skip to content
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

Ignore const fn in or_fun_call lint #5682

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1685,12 +1685,7 @@ fn lint_or_fun_call<'a, 'tcx>(
if path.ident.as_str() == "len" {
let ty = walk_ptrs_ty(cx.tables.expr_ty(&args[0]));

match ty.kind {
ty::Slice(_) | ty::Array(_, _) => return,
_ => (),
}

if match_type(cx, ty, &paths::VEC) {
if matches!(ty.kind, ty::Slice(_) | ty::Array(..)) || match_type(cx, ty, &paths::VEC) {
return;
}
}
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ pub fn is_ctor_or_promotable_const_function(cx: &LateContext<'_, '_>, expr: &Exp
if let ExprKind::Path(ref qp) = fun.kind {
let res = cx.tables.qpath_res(qp, fun.hir_id);
return match res {
def::Res::Def(DefKind::Fn, def_id) => rustc_mir::const_eval::is_const_fn(cx.tcx, def_id),
tesuji marked this conversation as resolved.
Show resolved Hide resolved
def::Res::Def(DefKind::Variant | DefKind::Ctor(..), ..) => true,
def::Res::Def(_, def_id) => cx.tcx.is_promotable_const_fn(def_id),
_ => false,
Expand Down
14 changes: 10 additions & 4 deletions tests/ui/or_fun_call.fixed
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// run-rustfix

#![warn(clippy::or_fun_call)]
#![allow(dead_code)]

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::time::Duration;

/// Checks implementation of the `OR_FUN_CALL` lint.
fn or_fun_call() {
pub fn or_fun_call() {
struct Foo;

impl Foo {
Expand Down Expand Up @@ -75,7 +74,7 @@ fn or_fun_call() {
struct Foo(u8);
struct Bar(String, Duration);
#[rustfmt::skip]
fn test_or_with_ctors() {
pub fn test_or_with_ctors() {
let opt = Some(1);
let opt_opt = Some(Some(1));
// we also test for const promotion, this makes sure we don't hit that
Expand Down Expand Up @@ -107,7 +106,7 @@ fn test_or_with_ctors() {
}

// Issue 4514 - early return
fn f() -> Option<()> {
pub fn f() -> Option<()> {
let a = Some(1);
let b = 1i32;

Expand All @@ -116,4 +115,11 @@ fn f() -> Option<()> {
Some(())
}

pub fn skip_const_fn() {
const fn foo(v: i32) -> Option<i32> {
Some(v)
}
let _ = None.or(foo(42));
}

fn main() {}
14 changes: 10 additions & 4 deletions tests/ui/or_fun_call.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// run-rustfix

#![warn(clippy::or_fun_call)]
#![allow(dead_code)]

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::time::Duration;

/// Checks implementation of the `OR_FUN_CALL` lint.
fn or_fun_call() {
pub fn or_fun_call() {
struct Foo;

impl Foo {
Expand Down Expand Up @@ -75,7 +74,7 @@ fn or_fun_call() {
struct Foo(u8);
struct Bar(String, Duration);
#[rustfmt::skip]
fn test_or_with_ctors() {
pub fn test_or_with_ctors() {
let opt = Some(1);
let opt_opt = Some(Some(1));
// we also test for const promotion, this makes sure we don't hit that
Expand Down Expand Up @@ -107,7 +106,7 @@ fn test_or_with_ctors() {
}

// Issue 4514 - early return
fn f() -> Option<()> {
pub fn f() -> Option<()> {
let a = Some(1);
let b = 1i32;

Expand All @@ -116,4 +115,11 @@ fn f() -> Option<()> {
Some(())
}

pub fn skip_const_fn() {
const fn foo(v: i32) -> Option<i32> {
Some(v)
}
let _ = None.or(foo(42));
}

fn main() {}
26 changes: 13 additions & 13 deletions tests/ui/or_fun_call.stderr
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:35:22
--> $DIR/or_fun_call.rs:34:22
|
LL | with_constructor.unwrap_or(make());
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
|
= note: `-D clippy::or-fun-call` implied by `-D warnings`

error: use of `unwrap_or` followed by a call to `new`
--> $DIR/or_fun_call.rs:38:5
--> $DIR/or_fun_call.rs:37:5
|
LL | with_new.unwrap_or(Vec::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`

error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:41:21
--> $DIR/or_fun_call.rs:40:21
|
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`

error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:44:14
--> $DIR/or_fun_call.rs:43:14
|
LL | with_err.unwrap_or(make());
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`

error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:47:19
--> $DIR/or_fun_call.rs:46:19
|
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`

error: use of `unwrap_or` followed by a call to `default`
--> $DIR/or_fun_call.rs:50:5
--> $DIR/or_fun_call.rs:49:5
|
LL | with_default_trait.unwrap_or(Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`

error: use of `unwrap_or` followed by a call to `default`
--> $DIR/or_fun_call.rs:53:5
--> $DIR/or_fun_call.rs:52:5
|
LL | with_default_type.unwrap_or(u64::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`

error: use of `unwrap_or` followed by a call to `new`
--> $DIR/or_fun_call.rs:56:5
--> $DIR/or_fun_call.rs:55:5
|
LL | with_vec.unwrap_or(vec![]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_vec.unwrap_or_default()`

error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:59:21
--> $DIR/or_fun_call.rs:58:21
|
LL | without_default.unwrap_or(Foo::new());
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`

error: use of `or_insert` followed by a function call
--> $DIR/or_fun_call.rs:62:19
--> $DIR/or_fun_call.rs:61:19
|
LL | map.entry(42).or_insert(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`

error: use of `or_insert` followed by a function call
--> $DIR/or_fun_call.rs:65:21
--> $DIR/or_fun_call.rs:64:21
|
LL | btree.entry(42).or_insert(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`

error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:68:21
--> $DIR/or_fun_call.rs:67:21
|
LL | let _ = stringy.unwrap_or("".to_owned());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`

error: use of `or` followed by a function call
--> $DIR/or_fun_call.rs:93:35
--> $DIR/or_fun_call.rs:92:35
|
LL | let _ = Some("a".to_string()).or(Some("b".to_string()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some("b".to_string()))`
Expand Down