Skip to content

Commit 3e108b7

Browse files
committed
Fix constant promotion stuff
1 parent 1f81dcb commit 3e108b7

File tree

2 files changed

+17
-67
lines changed

2 files changed

+17
-67
lines changed

clippy_lints/src/methods.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -821,18 +821,6 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
821821
or_has_args: bool,
822822
span: Span,
823823
) {
824-
// don't lint for constant values
825-
// FIXME: can we `expect` here instead of match?
826-
let promotable = cx.tcx
827-
.rvalue_promotable_to_static
828-
.borrow()
829-
.get(&arg.id)
830-
.cloned()
831-
.unwrap_or(true);
832-
if promotable {
833-
return;
834-
}
835-
836824
// (path, fn_has_argument, methods, suffix)
837825
let know_types: &[(&[_], _, &[_], _)] = &[
838826
(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
@@ -841,6 +829,22 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
841829
(&paths::RESULT, true, &["or", "unwrap_or"], "else"),
842830
];
843831

832+
// early check if the name is one we care about
833+
if know_types.iter().all(|k| !k.2.contains(&name)) {
834+
return;
835+
}
836+
837+
// don't lint for constant values
838+
// FIXME: can we `expect` here instead of match?
839+
let owner = cx.tcx.hir.get_parent(arg.id);
840+
let owner_def = cx.tcx.hir.local_def_id(owner);
841+
let promotable = cx.tcx
842+
.rvalue_promotable_map(owner_def)
843+
.contains_key(&arg.hir_id.local_id);
844+
if promotable {
845+
return;
846+
}
847+
844848
let self_ty = cx.tables.expr_ty(self_expr);
845849

846850
let (fn_has_arguments, poss, suffix) = if let Some(&(_, fn_has_arguments, poss, suffix)) =

tests/ui/methods.stderr

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -318,37 +318,13 @@ error: unnecessary structure name repetition
318318
263 | fn new() -> Foo { Foo }
319319
| ^^^ help: use the applicable keyword: `Self`
320320

321-
error: use of `unwrap_or` followed by a function call
322-
--> $DIR/methods.rs:281:5
323-
|
324-
281 | with_constructor.unwrap_or(make());
325-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_constructor.unwrap_or_else(make)`
326-
|
327-
= note: `-D or-fun-call` implied by `-D warnings`
328-
329321
error: use of `unwrap_or` followed by a call to `new`
330322
--> $DIR/methods.rs:284:5
331323
|
332324
284 | with_new.unwrap_or(Vec::new());
333325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
334-
335-
error: use of `unwrap_or` followed by a function call
336-
--> $DIR/methods.rs:287:5
337326
|
338-
287 | with_const_args.unwrap_or(Vec::with_capacity(12));
339-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_const_args.unwrap_or_else(|| Vec::with_capacity(12))`
340-
341-
error: use of `unwrap_or` followed by a function call
342-
--> $DIR/methods.rs:290:5
343-
|
344-
290 | with_err.unwrap_or(make());
345-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_err.unwrap_or_else(|_| make())`
346-
347-
error: use of `unwrap_or` followed by a function call
348-
--> $DIR/methods.rs:293:5
349-
|
350-
293 | with_err_args.unwrap_or(Vec::with_capacity(12));
351-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_err_args.unwrap_or_else(|_| Vec::with_capacity(12))`
327+
= note: `-D or-fun-call` implied by `-D warnings`
352328

353329
error: use of `unwrap_or` followed by a call to `default`
354330
--> $DIR/methods.rs:296:5
@@ -362,36 +338,6 @@ error: use of `unwrap_or` followed by a call to `default`
362338
299 | with_default_type.unwrap_or(u64::default());
363339
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
364340

365-
error: use of `unwrap_or` followed by a function call
366-
--> $DIR/methods.rs:302:5
367-
|
368-
302 | with_vec.unwrap_or(vec![]);
369-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_vec.unwrap_or_else(|| < [ _ ] > :: into_vec ( box [ $ ( $ x ) , * ] ))`
370-
371-
error: use of `unwrap_or` followed by a function call
372-
--> $DIR/methods.rs:307:5
373-
|
374-
307 | without_default.unwrap_or(Foo::new());
375-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `without_default.unwrap_or_else(Foo::new)`
376-
377-
error: use of `or_insert` followed by a function call
378-
--> $DIR/methods.rs:310:5
379-
|
380-
310 | map.entry(42).or_insert(String::new());
381-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `map.entry(42).or_insert_with(String::new)`
382-
383-
error: use of `or_insert` followed by a function call
384-
--> $DIR/methods.rs:313:5
385-
|
386-
313 | btree.entry(42).or_insert(String::new());
387-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `btree.entry(42).or_insert_with(String::new)`
388-
389-
error: use of `unwrap_or` followed by a function call
390-
--> $DIR/methods.rs:316:13
391-
|
392-
316 | let _ = stringy.unwrap_or("".to_owned());
393-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `stringy.unwrap_or_else(|| "".to_owned())`
394-
395341
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
396342
--> $DIR/methods.rs:327:23
397343
|

0 commit comments

Comments
 (0)