rust/clippy_lints/src
bors a59236ffb4 Auto merge of #10919 - y21:issue10579, r=blyxyas,xFrednet
[`map_unwrap_or`]: don't lint when referenced variable is moved

Fixes #10579.

The previous way of checking if changing `map(f).unwrap_or(a)` to `map_or(a, f)` is safe had a flaw when the argument to `unwrap_or` moves a binding and the `map` closure references that binding in some way.

It used to simply check if any of the identifiers in the `unwrap_or` argument are referenced in the `map` closure, but it didn't consider the case where the moved binding is referred to through references, for example:
```rs
let x = vec![1, 2];
let x_ref = &x;
Some(()).map(|_| x_ref.clone()).unwrap_or(x);
```
This compiles as is, but we cannot change it to `map_or`. This lint however did suggest changing it, because the simple way of checking if `x` is referenced anywhere in the `map` closure fails here. The safest thing to do here imo (what this PR does) is check if the moved value `x` is referenced *anywhere* in the body (before the `unwrap_or` call). One can always create a reference to the value and smuggle them into the closure, without actually referring to `x`. The original, linked issue shows another one such example:
```rs
    let x = vec![1,2,3,0];
    let y = x.strip_suffix(&[0]).map(|s| s.to_vec()).unwrap_or(x);
```
`x.strip_suffix(&[0])` creates a reference to `x` that is available through `s` inside of the `map` closure, so we can't change it to `map_or`.

changelog: [`map_unwrap_or`]: don't lint when referenced variable is moved
2023-06-14 09:48:22 +00:00
..
cargo
casts ignore more type aliases in unnecessary_cast 2023-06-11 06:59:01 -05:00
functions
loops Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
matches Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
methods Auto merge of #10919 - y21:issue10579, r=blyxyas,xFrednet 2023-06-14 09:48:22 +00:00
misc_early Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
operators
transmute
types
unit_types
utils Now cargo collect-metadata updates the CHANGELOG.md 2023-06-13 16:52:18 +02:00
allow_attributes.rs remove tuple 2023-06-03 14:38:16 -05:00
almost_complete_range.rs
approx_const.rs
arc_with_non_send_sync.rs Swapping to matches macro. 2023-06-08 00:38:18 -07:00
as_conversions.rs Don't ling as_conversions in proc macros 2023-06-08 19:04:35 +01:00
asm_syntax.rs
assertions_on_constants.rs
assertions_on_result_states.rs
async_yields_async.rs
attrs.rs Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
await_holding_invalid.rs
blocks_in_if_conditions.rs
bool_assert_comparison.rs
bool_to_int_with_if.rs
booleans.rs Auto merge of #10814 - y21:issue10743, r=llogiq 2023-06-03 15:57:36 +00:00
borrow_deref_ref.rs
box_default.rs
checked_conversions.rs
cognitive_complexity.rs
collapsible_if.rs
collection_is_never_read.rs
comparison_chain.rs
copies.rs
copy_iterator.rs
crate_in_macro_def.rs
create_dir.rs
dbg_macro.rs
declared_lints.rs Add the lint to the lib 2023-06-12 16:19:26 +01:00
default_constructed_unit_structs.rs Add lint to check lint formulation messages 2023-06-03 00:00:30 +05:30
default_instead_of_iter_empty.rs
default_numeric_fallback.rs Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
default_union_representation.rs
default.rs
deprecated_lints.rs
dereference.rs Auto merge of #10855 - Centri3:explicit_deref_methods, r=llogiq 2023-06-04 14:40:50 +00:00
derivable_impls.rs
derive.rs
disallowed_macros.rs
disallowed_methods.rs
disallowed_names.rs
disallowed_script_idents.rs
disallowed_types.rs
doc.rs
double_parens.rs
drop_forget_ref.rs
duplicate_mod.rs
else_if_without_else.rs
empty_drop.rs
empty_enum.rs
empty_structs_with_brackets.rs
endian_bytes.rs derive Copy/PartialEq for Prefix 2023-06-03 14:31:40 -05:00
entry.rs
enum_clike.rs
enum_variants.rs allow disabling module inception on private modules 2023-06-10 08:09:07 -05:00
equatable_if_let.rs
escape.rs
eta_reduction.rs actually don't lint for inclusive range 2023-06-08 00:34:23 +02:00
excessive_bools.rs
excessive_nesting.rs Update excessive_nesting.rs 2023-06-09 16:29:34 -05:00
exhaustive_items.rs
exit.rs
explicit_write.rs
extra_unused_type_parameters.rs
fallible_impl_from.rs
float_literal.rs
floating_point_arithmetic.rs
fn_null_check.rs
format_args.rs
format_impl.rs
format_push_string.rs
format.rs
formatting.rs Don't warn if there is a comment between else and curly bracket 2023-06-07 09:42:37 +01:00
from_over_into.rs
from_raw_with_void_ptr.rs
from_str_radix_10.rs
future_not_send.rs
if_let_mutex.rs
if_not_else.rs
if_then_some_else_none.rs
implicit_hasher.rs
implicit_return.rs
implicit_saturating_add.rs
implicit_saturating_sub.rs
inconsistent_struct_constructor.rs
index_refutable_slice.rs
indexing_slicing.rs
infinite_iter.rs
inherent_impl.rs
inherent_to_string.rs
init_numbered_fields.rs
inline_fn_without_body.rs
instant_subtraction.rs
int_plus_one.rs
invalid_upcast_comparisons.rs
items_after_statements.rs
items_after_test_module.rs
iter_not_returning_iterator.rs
large_const_arrays.rs
large_enum_variant.rs
large_futures.rs
large_include_file.rs
large_stack_arrays.rs
large_stack_frames.rs make lint description easier to read, prevent ICE 2023-06-12 12:21:38 +02:00
len_zero.rs
let_if_seq.rs
let_underscore.rs
let_with_type_underscore.rs check for _ instead 2023-06-06 21:01:14 -05:00
lib.deprecated.rs
lib.rs Auto merge of #10911 - lochetti:fix_9657, r=Alexendoo 2023-06-12 12:28:36 +00:00
lifetimes.rs [needless_lifetimes]' suggestion now points at the lifetimes 2023-06-13 14:05:22 -05:00
lines_filter_map_ok.rs
literal_representation.rs
macro_use.rs Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
main_recursion.rs
manual_assert.rs
manual_async_fn.rs
manual_bits.rs
manual_clamp.rs
manual_is_ascii_check.rs
manual_let_else.rs Support struct patterns 2023-06-02 14:46:27 +02:00
manual_main_separator_str.rs
manual_non_exhaustive.rs
manual_rem_euclid.rs
manual_retain.rs
manual_slice_size_calculation.rs
manual_string_new.rs
manual_strip.rs
map_unit_fn.rs
match_result_ok.rs
mem_forget.rs
mem_replace.rs
min_ident_chars.rs refactor and add link to issue 2023-06-12 03:22:01 -05:00
minmax.rs
misc.rs
mismatching_type_param_order.rs Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
missing_assert_message.rs
missing_const_for_fn.rs
missing_doc.rs
missing_enforced_import_rename.rs
missing_fields_in_debug.rs don't call type_of on generic params 2023-06-06 01:23:42 +02:00
missing_inline.rs
missing_trait_methods.rs
mixed_read_write_in_expression.rs refactor 2023-06-09 15:32:42 -05:00
module_style.rs
multi_assignments.rs
multiple_unsafe_ops_per_block.rs
mut_key.rs
mut_mut.rs
mut_reference.rs
mutable_debug_assertion.rs
mutex_atomic.rs
needless_arbitrary_self_type.rs
needless_bool.rs
needless_borrowed_ref.rs
needless_continue.rs
needless_else.rs
needless_for_each.rs
needless_if.rs Don't lint non-statement/faux empty needless_ifs 2023-06-12 15:19:00 +00:00
needless_late_init.rs
needless_parens_on_range_literals.rs
needless_pass_by_value.rs Remove dead code in needless_pass_by_value 2023-06-13 17:08:51 +02:00
needless_question_mark.rs
needless_update.rs
neg_cmp_op_on_partial_ord.rs
neg_multiply.rs
new_without_default.rs
no_effect.rs [no_effect]: suggest adding return if applicable 2023-06-13 13:23:33 -05:00
no_mangle_with_rust_abi.rs
non_copy_const.rs
non_expressive_names.rs
non_octal_unix_permissions.rs
non_send_fields_in_send_ty.rs
nonstandard_macro_braces.rs
octal_escapes.rs
only_used_in_recursion.rs
option_env_unwrap.rs
option_if_let_else.rs
overflow_check_conditional.rs
panic_in_result_fn.rs
panic_unimplemented.rs
partial_pub_fields.rs
partialeq_ne_impl.rs
partialeq_to_none.rs
pass_by_ref_or_value.rs
pattern_type_mismatch.rs
permissions_set_readonly_false.rs
precedence.rs
ptr_offset_with_cast.rs
ptr.rs
pub_use.rs
question_mark_used.rs
question_mark.rs
ranges.rs
rc_clone_in_vec_init.rs
read_zero_byte_vec.rs
redundant_async_block.rs
redundant_clone.rs Move redundant_clone to nursery 2023-06-02 15:13:55 +00:00
redundant_closure_call.rs
redundant_else.rs
redundant_field_names.rs
redundant_pub_crate.rs
redundant_slicing.rs
redundant_static_lifetimes.rs
redundant_type_annotations.rs Add Limitations section 2023-06-05 10:22:46 +02:00
ref_option_ref.rs
ref_patterns.rs
reference.rs
regex.rs
renamed_lints.rs
return_self_not_must_use.rs
returns.rs Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
same_name_method.rs
self_named_constructors.rs
semicolon_block.rs
semicolon_if_nothing_returned.rs
serde_api.rs
shadow.rs
significant_drop_tightening.rs Reborrow mutable references in explicit_iter_loop 2023-06-09 21:40:03 -04:00
single_char_lifetime_names.rs
single_component_path_imports.rs
size_of_in_element_count.rs
size_of_ref.rs
slow_vector_initialization.rs
std_instead_of_core.rs
strings.rs
strlen_on_c_strings.rs
suspicious_doc_comments.rs
suspicious_operation_groupings.rs
suspicious_trait_impl.rs
suspicious_xor_used_as_pow.rs
swap_ptr_to_ref.rs
swap.rs
tabs_in_doc_comments.rs
temporary_assignment.rs
tests_outside_test_module.rs
to_digit_is_some.rs
trailing_empty_array.rs
trait_bounds.rs Auto merge of #10894 - Centri3:type_repetition_in_bounds, r=blyxyas,xFrednet 2023-06-12 07:18:39 +00:00
undocumented_unsafe_blocks.rs
unicode.rs
uninit_vec.rs
unit_return_expecting_ord.rs
unnamed_address.rs
unnecessary_box_returns.rs
unnecessary_owned_empty_strings.rs
unnecessary_self_imports.rs
unnecessary_struct_initialization.rs
unnecessary_wraps.rs
unnested_or_patterns.rs
unsafe_removed_from_name.rs
unused_async.rs
unused_io_amount.rs
unused_peekable.rs
unused_rounding.rs
unused_self.rs
unused_unit.rs
unwrap_in_result.rs
unwrap.rs
upper_case_acronyms.rs
use_self.rs
useless_conversion.rs add plural form to useless_conversion if depth > 0 2023-06-03 19:38:55 +02:00
vec_init_then_push.rs
vec.rs [useless_vec]: detect unnecessary vec![_] 2023-06-12 15:48:04 +02:00
wildcard_imports.rs
write.rs
zero_div_zero.rs
zero_sized_map_values.rs