├── .cargo └── config ├── .editorconfig ├── .gitattributes ├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE ├── deploy.sh └── deploy_key.enc ├── .gitignore ├── .remarkrc.json ├── .travis.yml ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── COPYRIGHT ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── PUBLISH.md ├── README.md ├── appveyor.yml ├── build.rs ├── ci ├── base-tests.sh └── integration-tests.sh ├── clippy_dev ├── Cargo.toml └── src │ ├── lib.rs │ └── main.rs ├── clippy_dummy ├── Cargo.toml ├── PUBLISH.md ├── build.rs ├── crates-readme.md └── src │ └── main.rs ├── clippy_lints ├── Cargo.toml ├── README.md └── src │ ├── approx_const.rs │ ├── arithmetic.rs │ ├── assertions_on_constants.rs │ ├── assign_ops.rs │ ├── attrs.rs │ ├── bit_mask.rs │ ├── blacklisted_name.rs │ ├── block_in_if_condition.rs │ ├── booleans.rs │ ├── bytecount.rs │ ├── cargo_common_metadata.rs │ ├── cognitive_complexity.rs │ ├── collapsible_if.rs │ ├── const_static_lifetime.rs │ ├── consts.rs │ ├── copies.rs │ ├── copy_iterator.rs │ ├── dbg_macro.rs │ ├── default_trait_access.rs │ ├── deprecated_lints.rs │ ├── derive.rs │ ├── doc.rs │ ├── double_comparison.rs │ ├── double_parens.rs │ ├── drop_bounds.rs │ ├── drop_forget_ref.rs │ ├── duration_subsec.rs │ ├── else_if_without_else.rs │ ├── empty_enum.rs │ ├── entry.rs │ ├── enum_clike.rs │ ├── enum_glob_use.rs │ ├── enum_variants.rs │ ├── eq_op.rs │ ├── erasing_op.rs │ ├── escape.rs │ ├── eta_reduction.rs │ ├── eval_order_dependence.rs │ ├── excessive_precision.rs │ ├── explicit_write.rs │ ├── fallible_impl_from.rs │ ├── format.rs │ ├── formatting.rs │ ├── functions.rs │ ├── identity_conversion.rs │ ├── identity_op.rs │ ├── if_not_else.rs │ ├── implicit_return.rs │ ├── indexing_slicing.rs │ ├── infallible_destructuring_match.rs │ ├── infinite_iter.rs │ ├── inherent_impl.rs │ ├── inline_fn_without_body.rs │ ├── int_plus_one.rs │ ├── invalid_ref.rs │ ├── items_after_statements.rs │ ├── large_enum_variant.rs │ ├── len_zero.rs │ ├── let_if_seq.rs │ ├── lib.rs │ ├── lifetimes.rs │ ├── literal_representation.rs │ ├── loops.rs │ ├── map_clone.rs │ ├── map_unit_fn.rs │ ├── matches.rs │ ├── mem_discriminant.rs │ ├── mem_forget.rs │ ├── mem_replace.rs │ ├── methods │ ├── mod.rs │ ├── option_map_unwrap_or.rs │ └── unnecessary_filter_map.rs │ ├── minmax.rs │ ├── misc.rs │ ├── misc_early.rs │ ├── missing_const_for_fn.rs │ ├── missing_doc.rs │ ├── missing_inline.rs │ ├── multiple_crate_versions.rs │ ├── mut_mut.rs │ ├── mut_reference.rs │ ├── mutex_atomic.rs │ ├── needless_bool.rs │ ├── needless_borrow.rs │ ├── needless_borrowed_ref.rs │ ├── needless_continue.rs │ ├── needless_pass_by_value.rs │ ├── needless_update.rs │ ├── neg_cmp_op_on_partial_ord.rs │ ├── neg_multiply.rs │ ├── new_without_default.rs │ ├── no_effect.rs │ ├── non_copy_const.rs │ ├── non_expressive_names.rs │ ├── ok_if_let.rs │ ├── open_options.rs │ ├── overflow_check_conditional.rs │ ├── panic_unimplemented.rs │ ├── partialeq_ne_impl.rs │ ├── path_buf_push_overwrite.rs │ ├── precedence.rs │ ├── ptr.rs │ ├── ptr_offset_with_cast.rs │ ├── question_mark.rs │ ├── ranges.rs │ ├── redundant_clone.rs │ ├── redundant_field_names.rs │ ├── redundant_pattern_matching.rs │ ├── reference.rs │ ├── regex.rs │ ├── replace_consts.rs │ ├── returns.rs │ ├── serde_api.rs │ ├── shadow.rs │ ├── slow_vector_initialization.rs │ ├── strings.rs │ ├── suspicious_trait_impl.rs │ ├── swap.rs │ ├── temporary_assignment.rs │ ├── transmute.rs │ ├── transmuting_null.rs │ ├── trivially_copy_pass_by_ref.rs │ ├── types.rs │ ├── unicode.rs │ ├── unsafe_removed_from_name.rs │ ├── unused_io_amount.rs │ ├── unused_label.rs │ ├── unwrap.rs │ ├── use_self.rs │ ├── utils │ ├── attrs.rs │ ├── author.rs │ ├── camel_case.rs │ ├── comparisons.rs │ ├── conf.rs │ ├── constants.rs │ ├── diagnostics.rs │ ├── higher.rs │ ├── hir_utils.rs │ ├── inspector.rs │ ├── internal_lints.rs │ ├── mod.rs │ ├── paths.rs │ ├── ptr.rs │ ├── sugg.rs │ └── usage.rs │ ├── vec.rs │ ├── wildcard_dependencies.rs │ ├── write.rs │ └── zero_div_zero.rs ├── clippy_workspace_tests ├── Cargo.toml ├── src │ └── main.rs └── subcrate │ ├── Cargo.toml │ └── src │ └── lib.rs ├── doc └── adding_lints.md ├── etc └── relicense │ ├── RELICENSE_DOCUMENTATION.md │ ├── contributors.txt │ └── relicense_comments.txt ├── mini-macro ├── Cargo.toml └── src │ └── lib.rs ├── pre_publish.sh ├── publish.files ├── rust-toolchain ├── rustc_tools_util ├── Cargo.toml ├── README.md └── src │ └── lib.rs ├── rustfmt.toml ├── setup-toolchain.sh ├── src ├── driver.rs ├── lib.rs └── main.rs ├── tests ├── auxiliary │ └── test_macro.rs ├── compile-test.rs ├── dogfood.rs ├── matches.rs ├── missing-test-files.rs ├── needless_continue_helpers.rs ├── ui-toml │ ├── bad_toml │ │ ├── clippy.toml │ │ ├── conf_bad_toml.rs │ │ └── conf_bad_toml.stderr │ ├── bad_toml_type │ │ ├── clippy.toml │ │ ├── conf_bad_type.rs │ │ └── conf_bad_type.stderr │ ├── conf_deprecated_key │ │ ├── clippy.toml │ │ ├── conf_deprecated_key.rs │ │ └── conf_deprecated_key.stderr │ ├── functions_maxlines │ │ ├── clippy.toml │ │ ├── test.rs │ │ └── test.stderr │ ├── good_toml_no_false_negatives │ │ ├── clippy.toml │ │ └── conf_no_false_negatives.rs │ ├── toml_blacklist │ │ ├── clippy.toml │ │ ├── conf_french_blacklisted_name.rs │ │ └── conf_french_blacklisted_name.stderr │ ├── toml_trivially_copy │ │ ├── clippy.toml │ │ ├── test.rs │ │ └── test.stderr │ ├── toml_unknown_key │ │ ├── clippy.toml │ │ ├── conf_unknown_key.rs │ │ └── conf_unknown_key.stderr │ ├── update-all-references.sh │ └── update-references.sh ├── ui │ ├── absurd-extreme-comparisons.rs │ ├── absurd-extreme-comparisons.stderr │ ├── approx_const.rs │ ├── approx_const.stderr │ ├── arithmetic.rs │ ├── arithmetic.stderr │ ├── assertions_on_constants.rs │ ├── assertions_on_constants.stderr │ ├── assign_ops.rs │ ├── assign_ops.stderr │ ├── assign_ops2.rs │ ├── assign_ops2.stderr │ ├── attrs.rs │ ├── attrs.stderr │ ├── author.rs │ ├── author.stdout │ ├── author │ │ ├── call.rs │ │ ├── call.stdout │ │ ├── for_loop.rs │ │ ├── for_loop.stderr │ │ ├── for_loop.stdout │ │ ├── if.rs │ │ ├── if.stderr │ │ ├── if.stdout │ │ ├── matches.rs │ │ ├── matches.stderr │ │ └── matches.stout │ ├── auxiliary │ │ ├── option_helpers.rs │ │ └── proc_macro_derive.rs │ ├── bit_masks.rs │ ├── bit_masks.stderr │ ├── blacklisted_name.rs │ ├── blacklisted_name.stderr │ ├── block_in_if_condition.rs │ ├── block_in_if_condition.stderr │ ├── bool_comparison.fixed │ ├── bool_comparison.rs │ ├── bool_comparison.stderr │ ├── booleans.rs │ ├── booleans.stderr │ ├── borrow_box.rs │ ├── borrow_box.stderr │ ├── box_vec.rs │ ├── box_vec.stderr │ ├── builtin-type-shadow.rs │ ├── builtin-type-shadow.stderr │ ├── bytecount.rs │ ├── bytecount.stderr │ ├── cast.rs │ ├── cast.stderr │ ├── cast_alignment.rs │ ├── cast_alignment.stderr │ ├── cast_lossless_float.fixed │ ├── cast_lossless_float.rs │ ├── cast_lossless_float.stderr │ ├── cast_lossless_integer.fixed │ ├── cast_lossless_integer.rs │ ├── cast_lossless_integer.stderr │ ├── cast_ref_to_mut.rs │ ├── cast_ref_to_mut.stderr │ ├── cast_size.rs │ ├── cast_size.stderr │ ├── cfg_attr_rustfmt.fixed │ ├── cfg_attr_rustfmt.rs │ ├── cfg_attr_rustfmt.stderr │ ├── char_lit_as_u8.rs │ ├── char_lit_as_u8.stderr │ ├── checked_unwrap.rs │ ├── checked_unwrap.stderr │ ├── clone_on_copy_impl.rs │ ├── clone_on_copy_mut.rs │ ├── cmp_nan.rs │ ├── cmp_nan.stderr │ ├── cmp_null.rs │ ├── cmp_null.stderr │ ├── cmp_owned.rs │ ├── cmp_owned.stderr │ ├── cognitive_complexity.rs │ ├── cognitive_complexity.stderr │ ├── cognitive_complexity_attr_used.rs │ ├── cognitive_complexity_attr_used.stderr │ ├── collapsible_if.fixed │ ├── collapsible_if.rs │ ├── collapsible_if.stderr │ ├── complex_types.rs │ ├── complex_types.stderr │ ├── const_static_lifetime.rs │ ├── const_static_lifetime.stderr │ ├── copy_iterator.rs │ ├── copy_iterator.stderr │ ├── crashes │ │ ├── associated-constant-ice.rs │ │ ├── auxiliary │ │ │ └── proc_macro_crash.rs │ │ ├── cc_seme.rs │ │ ├── enum-glob-import-crate.rs │ │ ├── ice-1588.rs │ │ ├── ice-1782.rs │ │ ├── ice-1969.rs │ │ ├── ice-2499.rs │ │ ├── ice-2594.rs │ │ ├── ice-2727.rs │ │ ├── ice-2760.rs │ │ ├── ice-2774.rs │ │ ├── ice-2865.rs │ │ ├── ice-3151.rs │ │ ├── ice-3462.rs │ │ ├── ice-3741.rs │ │ ├── ice-3747.rs │ │ ├── ice-3891.rs │ │ ├── ice-3891.stderr │ │ ├── ice-700.rs │ │ ├── ice_exacte_size.rs │ │ ├── if_same_then_else.rs │ │ ├── issue-2862.rs │ │ ├── issue-825.rs │ │ ├── issues_loop_mut_cond.rs │ │ ├── match_same_arms_const.rs │ │ ├── mut_mut_macro.rs │ │ ├── needless_borrow_fp.rs │ │ ├── needless_lifetimes_impl_trait.rs │ │ ├── procedural_macro.rs │ │ ├── regressions.rs │ │ ├── returns.rs │ │ ├── single-match-else.rs │ │ ├── used_underscore_binding_macro.rs │ │ └── whitelist │ │ │ ├── clippy.toml │ │ │ └── conf_whitelisted.rs │ ├── cstring.rs │ ├── cstring.stderr │ ├── dbg_macro.rs │ ├── dbg_macro.stderr │ ├── decimal_literal_representation.rs │ ├── decimal_literal_representation.stderr │ ├── default_trait_access.rs │ ├── default_trait_access.stderr │ ├── default_trait_access.stdout │ ├── deprecated.rs │ ├── deprecated.stderr │ ├── deref_addrof.fixed │ ├── deref_addrof.rs │ ├── deref_addrof.stderr │ ├── deref_addrof_double_trigger.rs │ ├── deref_addrof_double_trigger.stderr │ ├── derive.rs │ ├── derive.stderr │ ├── diverging_sub_expression.rs │ ├── diverging_sub_expression.stderr │ ├── dlist.rs │ ├── dlist.stderr │ ├── doc.rs │ ├── doc.stderr │ ├── double_comparison.fixed │ ├── double_comparison.rs │ ├── double_comparison.stderr │ ├── double_neg.rs │ ├── double_neg.stderr │ ├── double_parens.rs │ ├── double_parens.stderr │ ├── drop_bounds.rs │ ├── drop_bounds.stderr │ ├── drop_forget_copy.rs │ ├── drop_forget_copy.stderr │ ├── drop_forget_ref.rs │ ├── drop_forget_ref.stderr │ ├── duplicate_underscore_argument.rs │ ├── duplicate_underscore_argument.stderr │ ├── duration_subsec.fixed │ ├── duration_subsec.rs │ ├── duration_subsec.stderr │ ├── duration_subsec.stdout │ ├── else_if_without_else.rs │ ├── else_if_without_else.stderr │ ├── empty_enum.rs │ ├── empty_enum.stderr │ ├── empty_line_after_outer_attribute.rs │ ├── empty_line_after_outer_attribute.stderr │ ├── entry.rs │ ├── entry.stderr │ ├── enum_glob_use.rs │ ├── enum_glob_use.stderr │ ├── enum_variants.rs │ ├── enum_variants.stderr │ ├── enums_clike.rs │ ├── enums_clike.stderr │ ├── eq_op.rs │ ├── eq_op.stderr │ ├── erasing_op.rs │ ├── erasing_op.stderr │ ├── escape_analysis.rs │ ├── escape_analysis.stderr │ ├── eta.fixed │ ├── eta.rs │ ├── eta.stderr │ ├── eval_order_dependence.rs │ ├── eval_order_dependence.stderr │ ├── excessive_precision.fixed │ ├── excessive_precision.rs │ ├── excessive_precision.stderr │ ├── expect_fun_call.fixed │ ├── expect_fun_call.rs │ ├── expect_fun_call.stderr │ ├── explicit_counter_loop.rs │ ├── explicit_counter_loop.stderr │ ├── explicit_write.fixed │ ├── explicit_write.rs │ ├── explicit_write.stderr │ ├── fallible_impl_from.rs │ ├── fallible_impl_from.stderr │ ├── filter_map_next.rs │ ├── filter_map_next.stderr │ ├── filter_methods.rs │ ├── filter_methods.stderr │ ├── find_map.rs │ ├── find_map.stderr │ ├── float_cmp.rs │ ├── float_cmp.stderr │ ├── float_cmp_const.rs │ ├── float_cmp_const.stderr │ ├── fn_to_numeric_cast.rs │ ├── fn_to_numeric_cast.stderr │ ├── for_kv_map.rs │ ├── for_kv_map.stderr │ ├── for_loop.rs │ ├── for_loop.stderr │ ├── for_loop.stdout │ ├── for_loop_over_option_result.rs │ ├── for_loop_over_option_result.stderr │ ├── format.fixed │ ├── format.rs │ ├── format.stderr │ ├── formatting.rs │ ├── formatting.stderr │ ├── functions.rs │ ├── functions.stderr │ ├── functions_maxlines.rs │ ├── functions_maxlines.stderr │ ├── get_unwrap.fixed │ ├── get_unwrap.rs │ ├── get_unwrap.stderr │ ├── ice-2636.rs │ ├── ice-2636.stderr │ ├── ice-360.rs │ ├── ice-360.stderr │ ├── ice-3717.rs │ ├── ice-3717.stderr │ ├── identity_conversion.rs │ ├── identity_conversion.stderr │ ├── identity_op.rs │ ├── identity_op.stderr │ ├── if_not_else.rs │ ├── if_not_else.stderr │ ├── if_same_then_else.rs │ ├── if_same_then_else.stderr │ ├── ifs_same_cond.rs │ ├── ifs_same_cond.stderr │ ├── impl.rs │ ├── impl.stderr │ ├── implicit_hasher.rs │ ├── implicit_hasher.stderr │ ├── implicit_return.rs │ ├── implicit_return.stderr │ ├── inconsistent_digit_grouping.fixed │ ├── inconsistent_digit_grouping.rs │ ├── inconsistent_digit_grouping.stderr │ ├── indexing_slicing.rs │ ├── indexing_slicing.stderr │ ├── infallible_destructuring_match.fixed │ ├── infallible_destructuring_match.rs │ ├── infallible_destructuring_match.stderr │ ├── infinite_iter.rs │ ├── infinite_iter.stderr │ ├── infinite_loop.rs │ ├── infinite_loop.stderr │ ├── inline_fn_without_body.rs │ ├── inline_fn_without_body.stderr │ ├── int_plus_one.rs │ ├── int_plus_one.stderr │ ├── into_iter_on_ref.fixed │ ├── into_iter_on_ref.rs │ ├── into_iter_on_ref.stderr │ ├── invalid_ref.rs │ ├── invalid_ref.stderr │ ├── invalid_upcast_comparisons.rs │ ├── invalid_upcast_comparisons.stderr │ ├── issue-3145.rs │ ├── issue-3145.stderr │ ├── issue_2356.rs │ ├── issue_2356.stderr │ ├── issue_3849.rs │ ├── issue_3849.stdout │ ├── item_after_statement.rs │ ├── item_after_statement.stderr │ ├── iter_nth.rs │ ├── iter_nth.stderr │ ├── iter_skip_next.rs │ ├── iter_skip_next.stderr │ ├── large_digit_groups.fixed │ ├── large_digit_groups.rs │ ├── large_digit_groups.stderr │ ├── large_enum_variant.rs │ ├── large_enum_variant.stderr │ ├── len_without_is_empty.rs │ ├── len_without_is_empty.stderr │ ├── len_zero.fixed │ ├── len_zero.rs │ ├── len_zero.stderr │ ├── let_if_seq.rs │ ├── let_if_seq.stderr │ ├── let_return.rs │ ├── let_return.stderr │ ├── let_unit.rs │ ├── let_unit.stderr │ ├── lifetimes.rs │ ├── lifetimes.stderr │ ├── lint_without_lint_pass.rs │ ├── lint_without_lint_pass.stderr │ ├── literals.rs │ ├── literals.stderr │ ├── manual_memcpy.rs │ ├── manual_memcpy.stderr │ ├── map_clone.fixed │ ├── map_clone.rs │ ├── map_clone.stderr │ ├── map_flatten.rs │ ├── map_flatten.stderr │ ├── map_unit_fn.rs │ ├── match_as_ref.fixed │ ├── match_as_ref.rs │ ├── match_as_ref.stderr │ ├── match_bool.rs │ ├── match_bool.stderr │ ├── match_overlapping_arm.rs │ ├── match_overlapping_arm.stderr │ ├── match_same_arms.rs │ ├── match_same_arms.stderr │ ├── matches.rs │ ├── matches.stderr │ ├── mem_discriminant.rs │ ├── mem_discriminant.stderr │ ├── mem_forget.rs │ ├── mem_forget.stderr │ ├── mem_replace.fixed │ ├── mem_replace.rs │ ├── mem_replace.stderr │ ├── methods.rs │ ├── methods.stderr │ ├── min_max.rs │ ├── min_max.stderr │ ├── missing-doc-crate-missing.rs │ ├── missing-doc-crate-missing.stderr │ ├── missing-doc-crate.rs │ ├── missing-doc-crate.stderr │ ├── missing-doc.rs │ ├── missing-doc.stderr │ ├── missing_const_for_fn │ │ ├── cant_be_const.rs │ │ ├── cant_be_const.stderr │ │ ├── could_be_const.rs │ │ └── could_be_const.stderr │ ├── missing_inline.rs │ ├── missing_inline.stderr │ ├── mistyped_literal_suffix.fixed │ ├── mistyped_literal_suffix.rs │ ├── mistyped_literal_suffix.stderr │ ├── module_inception.rs │ ├── module_inception.stderr │ ├── module_name_repetitions.rs │ ├── module_name_repetitions.stderr │ ├── modulo_one.rs │ ├── modulo_one.stderr │ ├── mut_from_ref.rs │ ├── mut_from_ref.stderr │ ├── mut_mut.rs │ ├── mut_mut.stderr │ ├── mut_range_bound.rs │ ├── mut_range_bound.stderr │ ├── mut_reference.rs │ ├── mut_reference.stderr │ ├── mutex_atomic.rs │ ├── mutex_atomic.stderr │ ├── my_lint.rs │ ├── needless_bool.rs │ ├── needless_bool.stderr │ ├── needless_borrow.rs │ ├── needless_borrow.stderr │ ├── needless_borrowed_ref.rs │ ├── needless_borrowed_ref.stderr │ ├── needless_collect.rs │ ├── needless_collect.stderr │ ├── needless_continue.rs │ ├── needless_continue.stderr │ ├── needless_pass_by_value.rs │ ├── needless_pass_by_value.stderr │ ├── needless_pass_by_value_proc_macro.rs │ ├── needless_range_loop.rs │ ├── needless_range_loop.stderr │ ├── needless_return.rs │ ├── needless_return.stderr │ ├── needless_update.rs │ ├── needless_update.stderr │ ├── neg_cmp_op_on_partial_ord.rs │ ├── neg_cmp_op_on_partial_ord.stderr │ ├── neg_multiply.rs │ ├── neg_multiply.stderr │ ├── never_loop.rs │ ├── never_loop.stderr │ ├── new_ret_no_self.rs │ ├── new_ret_no_self.stderr │ ├── new_without_default.rs │ ├── new_without_default.stderr │ ├── no_effect.rs │ ├── no_effect.stderr │ ├── non_copy_const.rs │ ├── non_copy_const.stderr │ ├── non_expressive_names.rs │ ├── non_expressive_names.stderr │ ├── non_expressive_names.stdout │ ├── ok_expect.rs │ ├── ok_expect.stderr │ ├── ok_if_let.rs │ ├── ok_if_let.stderr │ ├── op_ref.rs │ ├── op_ref.stderr │ ├── open_options.rs │ ├── open_options.stderr │ ├── option_map_or_none.fixed │ ├── option_map_or_none.rs │ ├── option_map_or_none.stderr │ ├── option_map_unit_fn.rs │ ├── option_map_unit_fn.stderr │ ├── option_option.rs │ ├── option_option.stderr │ ├── or_fun_call.rs │ ├── or_fun_call.stderr │ ├── overflow_check_conditional.rs │ ├── overflow_check_conditional.stderr │ ├── panic_unimplemented.rs │ ├── panic_unimplemented.stderr │ ├── partialeq_ne_impl.rs │ ├── partialeq_ne_impl.stderr │ ├── path_buf_push_overwrite.fixed │ ├── path_buf_push_overwrite.rs │ ├── path_buf_push_overwrite.stderr │ ├── patterns.rs │ ├── patterns.stderr │ ├── precedence.fixed │ ├── precedence.rs │ ├── precedence.stderr │ ├── print.rs │ ├── print.stderr │ ├── print_literal.rs │ ├── print_literal.stderr │ ├── print_with_newline.rs │ ├── print_with_newline.stderr │ ├── println_empty_string.fixed │ ├── println_empty_string.rs │ ├── println_empty_string.stderr │ ├── proc_macro.rs │ ├── proc_macro.stderr │ ├── ptr_arg.rs │ ├── ptr_arg.stderr │ ├── ptr_offset_with_cast.fixed │ ├── ptr_offset_with_cast.rs │ ├── ptr_offset_with_cast.stderr │ ├── question_mark.rs │ ├── question_mark.stderr │ ├── range.rs │ ├── range.stderr │ ├── range_plus_minus_one.rs │ ├── range_plus_minus_one.stderr │ ├── redundant_clone.rs │ ├── redundant_clone.stderr │ ├── redundant_closure_call.rs │ ├── redundant_closure_call.stderr │ ├── redundant_field_names.fixed │ ├── redundant_field_names.rs │ ├── redundant_field_names.stderr │ ├── redundant_pattern_matching.rs │ ├── redundant_pattern_matching.stderr │ ├── regex.rs │ ├── regex.stderr │ ├── rename.rs │ ├── rename.stderr │ ├── renamed_builtin_attr.rs │ ├── renamed_builtin_attr.stderr │ ├── replace_consts.fixed │ ├── replace_consts.rs │ ├── replace_consts.stderr │ ├── result_map_unit_fn.rs │ ├── result_map_unit_fn.stderr │ ├── result_map_unwrap_or_else.rs │ ├── result_map_unwrap_or_else.stderr │ ├── serde.rs │ ├── serde.stderr │ ├── shadow.rs │ ├── shadow.stderr │ ├── short_circuit_statement.rs │ ├── short_circuit_statement.stderr │ ├── similar_names.rs │ ├── similar_names.stderr │ ├── single_char_pattern.fixed │ ├── single_char_pattern.rs │ ├── single_char_pattern.stderr │ ├── single_match.rs │ ├── single_match.stderr │ ├── single_match_else.rs │ ├── single_match_else.stderr │ ├── slow_vector_initialization.rs │ ├── slow_vector_initialization.stderr │ ├── slow_vector_initialization.stdout │ ├── starts_ends_with.fixed │ ├── starts_ends_with.rs │ ├── starts_ends_with.stderr │ ├── string_extend.fixed │ ├── string_extend.rs │ ├── string_extend.stderr │ ├── string_lit_as_bytes.fixed │ ├── string_lit_as_bytes.rs │ ├── string_lit_as_bytes.stderr │ ├── strings.rs │ ├── strings.stderr │ ├── suspicious_arithmetic_impl.rs │ ├── suspicious_arithmetic_impl.stderr │ ├── swap.rs │ ├── swap.stderr │ ├── temporary_assignment.rs │ ├── temporary_assignment.stderr │ ├── toplevel_ref_arg.rs │ ├── toplevel_ref_arg.stderr │ ├── trailing_zeros.rs │ ├── trailing_zeros.stderr │ ├── trailing_zeros.stdout │ ├── transmute.rs │ ├── transmute.stderr │ ├── transmute_32bit.rs │ ├── transmute_64bit.rs │ ├── transmute_64bit.stderr │ ├── transmuting_null.rs │ ├── transmuting_null.stderr │ ├── trivially_copy_pass_by_ref.rs │ ├── trivially_copy_pass_by_ref.stderr │ ├── ty_fn_sig.rs │ ├── ty_fn_sig.stderr │ ├── types.fixed │ ├── types.rs │ ├── types.stderr │ ├── unicode.rs │ ├── unicode.stderr │ ├── unit_arg.fixed │ ├── unit_arg.rs │ ├── unit_arg.stderr │ ├── unit_cmp.rs │ ├── unit_cmp.stderr │ ├── unknown_attribute.rs │ ├── unknown_attribute.stderr │ ├── unknown_clippy_lints.rs │ ├── unknown_clippy_lints.stderr │ ├── unnecessary_clone.rs │ ├── unnecessary_clone.stderr │ ├── unnecessary_filter_map.rs │ ├── unnecessary_filter_map.stderr │ ├── unnecessary_fold.fixed │ ├── unnecessary_fold.rs │ ├── unnecessary_fold.stderr │ ├── unnecessary_operation.rs │ ├── unnecessary_operation.stderr │ ├── unnecessary_ref.fixed │ ├── unnecessary_ref.rs │ ├── unnecessary_ref.stderr │ ├── unneeded_field_pattern.rs │ ├── unneeded_field_pattern.stderr │ ├── unreadable_literal.fixed │ ├── unreadable_literal.rs │ ├── unreadable_literal.stderr │ ├── unsafe_removed_from_name.rs │ ├── unsafe_removed_from_name.stderr │ ├── unused_io_amount.rs │ ├── unused_io_amount.stderr │ ├── unused_labels.rs │ ├── unused_labels.stderr │ ├── unused_lt.rs │ ├── unused_lt.stderr │ ├── unused_unit.fixed │ ├── unused_unit.rs │ ├── unused_unit.stderr │ ├── unwrap_or.rs │ ├── unwrap_or.stderr │ ├── unwrap_or.stdout │ ├── update-all-references.sh │ ├── update-references.sh │ ├── use_self.fixed │ ├── use_self.rs │ ├── use_self.stderr │ ├── used_underscore_binding.rs │ ├── used_underscore_binding.stderr │ ├── useful_asref.rs │ ├── useless_asref.fixed │ ├── useless_asref.rs │ ├── useless_asref.stderr │ ├── useless_attribute.rs │ ├── useless_attribute.stderr │ ├── vec.fixed │ ├── vec.rs │ ├── vec.stderr │ ├── vec_box_sized.fixed │ ├── vec_box_sized.rs │ ├── vec_box_sized.stderr │ ├── while_let_loop.rs │ ├── while_let_loop.stderr │ ├── while_let_on_iterator.rs │ ├── while_let_on_iterator.stderr │ ├── wildcard_enum_match_arm.rs │ ├── wildcard_enum_match_arm.stderr │ ├── write_literal.rs │ ├── write_literal.stderr │ ├── write_with_newline.rs │ ├── write_with_newline.stderr │ ├── writeln_empty_string.fixed │ ├── writeln_empty_string.rs │ ├── writeln_empty_string.stderr │ ├── wrong_self_convention.rs │ ├── wrong_self_convention.stderr │ ├── zero_div_zero.rs │ ├── zero_div_zero.stderr │ ├── zero_ptr.rs │ └── zero_ptr.stderr └── versioncheck.rs └── util ├── cov.sh ├── dev ├── export.py ├── fetch_prs_between.sh ├── gh-pages ├── index.html └── versions.html ├── lintlib.py └── update_lints.py /.cargo/config: -------------------------------------------------------------------------------- 1 | [alias] 2 | uitest = "test --test compile-test" 3 | 4 | [build] 5 | rustflags = ["-Zunstable-options"] 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | end_of_line = lf 9 | charset = utf-8 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | indent_style = space 13 | indent_size = 4 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | [attr]rust text eol=lf whitespace=tab-in-indent,trailing-space,tabwidth=4 2 | 3 | * text=auto eol=lf 4 | *.rs rust 5 | *.fixed linguist-language=Rust 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 9 | -------------------------------------------------------------------------------- /.github/deploy_key.enc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/.github/deploy_key.enc -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Used by Travis to be able to push: 2 | /.github/deploy_key 3 | out 4 | 5 | # Compiled files 6 | *.o 7 | *.d 8 | *.so 9 | *.rlib 10 | *.dll 11 | *.pyc 12 | *.rmeta 13 | 14 | # Executables 15 | *.exe 16 | 17 | # Generated by Cargo 18 | *Cargo.lock 19 | /target 20 | /clippy_lints/target 21 | /clippy_workspace_tests/target 22 | /clippy_dev/target 23 | /rustc_tools_util/target 24 | 25 | # Generated by dogfood 26 | /target_recur/ 27 | 28 | # gh pages docs 29 | util/gh-pages/lints.json 30 | 31 | # rustfmt backups 32 | *.rs.bk 33 | 34 | helper.txt 35 | *.iml 36 | .vscode 37 | .idea 38 | -------------------------------------------------------------------------------- /.remarkrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": { 3 | "lint": { 4 | "table-pipes": false, 5 | "table-pipe-alignment": false, 6 | "maximum-line-length": 120 7 | } 8 | }, 9 | "settings": { 10 | "commonmark": true 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | Copyright 2014-2019 The Rust Project Developers 2 | 3 | Licensed under the Apache License, Version 2.0 or the MIT license 5 | , at your 6 | option. All files in the project carrying such notice may not be 7 | copied, modified, or distributed except according to those terms. 8 | -------------------------------------------------------------------------------- /PUBLISH.md: -------------------------------------------------------------------------------- 1 | Steps to publish a new Clippy version 2 | 3 | - Bump `package.version` in `./Cargo.toml` (no need to manually bump `dependencies.clippy_lints.version`). 4 | - Write a changelog entry. 5 | - Run `./pre_publish.sh` 6 | - Review and commit all changed files 7 | - `git push` 8 | - Wait for Travis's approval. 9 | - Merge. 10 | - `cargo publish` in `./clippy_lints`. 11 | - `cargo publish` in the root directory. 12 | - `git pull`. 13 | - `git tag -s v0.0.X -m "v0.0.X"`. 14 | - `git push --tags`. 15 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // Forward the profile to the main compilation 3 | println!("cargo:rustc-env=PROFILE={}", std::env::var("PROFILE").unwrap()); 4 | // Don't rebuild even if nothing changed 5 | println!("cargo:rerun-if-changed=build.rs"); 6 | // forward git repo hashes we build at 7 | println!( 8 | "cargo:rustc-env=GIT_HASH={}", 9 | rustc_tools_util::get_commit_hash().unwrap_or_default() 10 | ); 11 | println!( 12 | "cargo:rustc-env=COMMIT_DATE={}", 13 | rustc_tools_util::get_commit_date().unwrap_or_default() 14 | ); 15 | } 16 | -------------------------------------------------------------------------------- /ci/integration-tests.sh: -------------------------------------------------------------------------------- 1 | set -x 2 | rm ~/.cargo/bin/cargo-clippy 3 | cargo install --force --path . 4 | 5 | echo "Running integration test for crate ${INTEGRATION}" 6 | 7 | git clone --depth=1 https://github.com/${INTEGRATION}.git checkout 8 | cd checkout 9 | 10 | function check() { 11 | # run clippy on a project, try to be verbose and trigger as many warnings as possible for greater coverage 12 | RUST_BACKTRACE=full cargo clippy --all-targets --all-features -- --cap-lints warn -W clippy::pedantic -W clippy::nursery &> clippy_output 13 | cat clippy_output 14 | ! cat clippy_output | grep -q "internal compiler error\|query stack during panic\|E0463" 15 | if [[ $? != 0 ]]; then 16 | return 1 17 | fi 18 | } 19 | 20 | case ${INTEGRATION} in 21 | *) 22 | check 23 | ;; 24 | esac 25 | -------------------------------------------------------------------------------- /clippy_dev/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "clippy_dev" 3 | version = "0.0.1" 4 | authors = ["Philipp Hansch "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | clap = "~2.32" 9 | itertools = "0.8" 10 | regex = "1" 11 | lazy_static = "1.0" 12 | walkdir = "2" 13 | -------------------------------------------------------------------------------- /clippy_dummy/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "clippy_dummy" # rename to clippy before publishing 3 | version = "0.0.302" 4 | authors = ["Manish Goregaokar "] 5 | edition = "2018" 6 | readme = "crates-readme.md" 7 | description = "A bunch of helpful lints to avoid common pitfalls in Rust." 8 | build = 'build.rs' 9 | 10 | repository = "https://github.com/rust-lang/rust-clippy" 11 | 12 | license = "MIT/Apache-2.0" 13 | keywords = ["clippy", "lint", "plugin"] 14 | categories = ["development-tools", "development-tools::cargo-plugins"] 15 | 16 | [build-dependencies] 17 | term = "0.5.1" 18 | -------------------------------------------------------------------------------- /clippy_dummy/PUBLISH.md: -------------------------------------------------------------------------------- 1 | This is a dummy crate to publish to crates.io. It primarily exists to ensure that folks trying to install clippy from crates.io get redirected to the `rustup` technique. 2 | 3 | Before publishing, be sure to rename `clippy_dummy` to `clippy` in `Cargo.toml`, it has a different name to avoid workspace issues. 4 | -------------------------------------------------------------------------------- /clippy_dummy/crates-readme.md: -------------------------------------------------------------------------------- 1 | Installing clippy via crates.io is deprecated. Please use the following: 2 | 3 | ```terminal 4 | rustup component add clippy-preview 5 | ``` 6 | 7 | on a Rust version 1.29 or later. You may need to run `rustup self update` if it complains about a missing clippy binary. 8 | 9 | See [the homepage](https://github.com/rust-lang/rust-clippy/#clippy) for more information 10 | -------------------------------------------------------------------------------- /clippy_dummy/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | panic!("This shouldn't even compile") 3 | } 4 | -------------------------------------------------------------------------------- /clippy_lints/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "clippy_lints" 3 | # begin automatic update 4 | version = "0.0.212" 5 | # end automatic update 6 | authors = [ 7 | "Manish Goregaokar ", 8 | "Andre Bogus ", 9 | "Georg Brandl ", 10 | "Martin Carton " 11 | ] 12 | description = "A bunch of helpful lints to avoid common pitfalls in Rust" 13 | repository = "https://github.com/rust-lang/rust-clippy" 14 | readme = "README.md" 15 | license = "MPL-2.0" 16 | keywords = ["clippy", "lint", "plugin"] 17 | edition = "2018" 18 | 19 | [dependencies] 20 | cargo_metadata = "0.7.1" 21 | itertools = "0.8" 22 | lazy_static = "1.0.2" 23 | matches = "0.1.7" 24 | quine-mc_cluskey = "0.2.2" 25 | regex-syntax = "0.6" 26 | semver = "0.9.0" 27 | serde = { version = "1.0", features = ["derive"] } 28 | toml = "0.5" 29 | unicode-normalization = "0.1" 30 | pulldown-cmark = "0.5.0" 31 | url = "1.7.0" 32 | if_chain = "0.1.3" 33 | smallvec = { version = "0.6.5", features = ["union"] } 34 | 35 | [features] 36 | debugging = [] 37 | -------------------------------------------------------------------------------- /clippy_lints/README.md: -------------------------------------------------------------------------------- 1 | This crate contains Clippy lints. For the main crate, check 2 | [*crates.io*](https://crates.io/crates/clippy) or 3 | [GitHub](https://github.com/rust-lang/rust-clippy). 4 | -------------------------------------------------------------------------------- /clippy_lints/src/utils/comparisons.rs: -------------------------------------------------------------------------------- 1 | //! Utility functions about comparison operators. 2 | 3 | #![deny(clippy::missing_docs_in_private_items)] 4 | 5 | use rustc::hir::{BinOpKind, Expr}; 6 | 7 | #[derive(PartialEq, Eq, Debug, Copy, Clone)] 8 | /// Represent a normalized comparison operator. 9 | pub enum Rel { 10 | /// `<` 11 | Lt, 12 | /// `<=` 13 | Le, 14 | /// `==` 15 | Eq, 16 | /// `!=` 17 | Ne, 18 | } 19 | 20 | /// Put the expression in the form `lhs < rhs`, `lhs <= rhs`, `lhs == rhs` or 21 | /// `lhs != rhs`. 22 | pub fn normalize_comparison<'a>(op: BinOpKind, lhs: &'a Expr, rhs: &'a Expr) -> Option<(Rel, &'a Expr, &'a Expr)> { 23 | match op { 24 | BinOpKind::Lt => Some((Rel::Lt, lhs, rhs)), 25 | BinOpKind::Le => Some((Rel::Le, lhs, rhs)), 26 | BinOpKind::Gt => Some((Rel::Lt, rhs, lhs)), 27 | BinOpKind::Ge => Some((Rel::Le, rhs, lhs)), 28 | BinOpKind::Eq => Some((Rel::Eq, rhs, lhs)), 29 | BinOpKind::Ne => Some((Rel::Ne, rhs, lhs)), 30 | _ => None, 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /clippy_lints/src/utils/constants.rs: -------------------------------------------------------------------------------- 1 | //! This module contains some useful constants. 2 | 3 | #![deny(clippy::missing_docs_in_private_items)] 4 | 5 | /// List of the built-in types names. 6 | /// 7 | /// See also [the reference][reference-types] for a list of such types. 8 | /// 9 | /// [reference-types]: https://doc.rust-lang.org/reference/types.html 10 | pub const BUILTIN_TYPES: &[&str] = &[ 11 | "i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64", "i128", "u128", "isize", "usize", "f32", "f64", "bool", 12 | "str", "char", 13 | ]; 14 | -------------------------------------------------------------------------------- /clippy_workspace_tests/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "clippy_workspace_tests" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | [workspace] 7 | members = ["subcrate"] 8 | -------------------------------------------------------------------------------- /clippy_workspace_tests/src/main.rs: -------------------------------------------------------------------------------- 1 | #![deny(rust_2018_idioms)] 2 | 3 | fn main() { 4 | } 5 | -------------------------------------------------------------------------------- /clippy_workspace_tests/subcrate/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "subcrate" 3 | version = "0.1.0" 4 | -------------------------------------------------------------------------------- /clippy_workspace_tests/subcrate/src/lib.rs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/clippy_workspace_tests/subcrate/src/lib.rs -------------------------------------------------------------------------------- /mini-macro/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "clippy-mini-macro-test" 3 | version = "0.2.0" 4 | authors = [ 5 | "Manish Goregaokar ", 6 | "Andre Bogus ", 7 | "Georg Brandl ", 8 | "Martin Carton ", 9 | "Oliver Schneider " 10 | ] 11 | license = "MPL-2.0" 12 | description = "A macro to test clippy's procedural macro checks" 13 | repository = "https://github.com/rust-lang/rust-clippy" 14 | edition = "2018" 15 | 16 | [lib] 17 | name = "clippy_mini_macro_test" 18 | proc-macro = true 19 | 20 | [dependencies] 21 | -------------------------------------------------------------------------------- /mini-macro/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(proc_macro_quote, proc_macro_hygiene)] 2 | #![deny(rust_2018_idioms)] 3 | extern crate proc_macro; 4 | 5 | use proc_macro::{TokenStream, quote}; 6 | 7 | #[proc_macro_derive(ClippyMiniMacroTest)] 8 | pub fn mini_macro(_: TokenStream) -> TokenStream { 9 | quote!( 10 | #[allow(unused)] fn needless_take_by_value(s: String) { println!("{}", s.len()); } 11 | #[allow(unused)] fn needless_loop(items: &[u8]) { 12 | for i in 0..items.len() { 13 | println!("{}", items[i]); 14 | } 15 | } 16 | ) 17 | } 18 | -------------------------------------------------------------------------------- /pre_publish.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | ./util/update_lints.py 6 | 7 | # add all changed files 8 | git add . 9 | git commit -m "Bump the version" 10 | 11 | set +e 12 | 13 | echo "Running \`cargo fmt\`.." 14 | 15 | cd clippy_lints && cargo fmt -- --write-mode=overwrite && cd .. 16 | cargo fmt -- --write-mode=overwrite 17 | 18 | echo "Running tests to make sure \`cargo fmt\` did not break anything.." 19 | 20 | cargo test 21 | 22 | echo "If the tests passed, review and commit the formatting changes and remember to add a git tag." 23 | -------------------------------------------------------------------------------- /publish.files: -------------------------------------------------------------------------------- 1 | M Cargo.toml 2 | M CHANGELOG.md 3 | M clippy_lints/Cargo.toml 4 | -------------------------------------------------------------------------------- /rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly 2 | -------------------------------------------------------------------------------- /rustc_tools_util/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rustc_tools_util" 3 | version = "0.1.1" 4 | authors = ["Matthias Krüger "] 5 | description = "small helper to generate version information for git packages" 6 | repository = "https://github.com/rust-lang/rust-clippy" 7 | readme = "README.md" 8 | license = "MIT/Apache-2.0" 9 | keywords = ["rustc", "tool", "git", "version", "hash"] 10 | categories = ["development-tools"] 11 | edition = "2018" 12 | [dependencies] 13 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | max_width = 120 2 | comment_width = 100 3 | match_block_trailing_comma = true 4 | wrap_comments = true 5 | 6 | error_on_line_overflow = true 7 | -------------------------------------------------------------------------------- /setup-toolchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Set up the appropriate rustc toolchain 3 | 4 | cd $(dirname $0) 5 | 6 | cargo install rustup-toolchain-install-master --debug || echo "rustup-toolchain-install-master already installed" 7 | RUSTC_HASH=$(git ls-remote https://github.com/rust-lang/rust.git master | awk '{print $1}') 8 | rustup-toolchain-install-master -f -n master $RUSTC_HASH 9 | rustup override set master 10 | -------------------------------------------------------------------------------- /tests/auxiliary/test_macro.rs: -------------------------------------------------------------------------------- 1 | pub trait A {} 2 | 3 | macro_rules! __implicit_hasher_test_macro { 4 | (impl< $($impl_arg:tt),* > for $kind:ty where $($bounds:tt)*) => { 5 | __implicit_hasher_test_macro!( ($($impl_arg),*) ($kind) ($($bounds)*) ); 6 | }; 7 | 8 | (($($impl_arg:tt)*) ($($kind_arg:tt)*) ($($bounds:tt)*)) => { 9 | impl< $($impl_arg)* > test_macro::A for $($kind_arg)* where $($bounds)* { } 10 | }; 11 | } 12 | -------------------------------------------------------------------------------- /tests/ui-toml/bad_toml/clippy.toml: -------------------------------------------------------------------------------- 1 | fn this_is_obviously(not: a, toml: file) { 2 | } 3 | -------------------------------------------------------------------------------- /tests/ui-toml/bad_toml/conf_bad_toml.rs: -------------------------------------------------------------------------------- 1 | // error-pattern: error reading Clippy's configuration file 2 | 3 | fn main() {} 4 | -------------------------------------------------------------------------------- /tests/ui-toml/bad_toml/conf_bad_toml.stderr: -------------------------------------------------------------------------------- 1 | error: error reading Clippy's configuration file `$DIR/clippy.toml`: expected an equals, found an identifier at line 1 2 | 3 | error: aborting due to previous error 4 | 5 | -------------------------------------------------------------------------------- /tests/ui-toml/bad_toml_type/clippy.toml: -------------------------------------------------------------------------------- 1 | blacklisted-names = 42 2 | -------------------------------------------------------------------------------- /tests/ui-toml/bad_toml_type/conf_bad_type.rs: -------------------------------------------------------------------------------- 1 | // error-pattern: error reading Clippy's configuration file: `blacklisted-names` is expected to be a 2 | // `Vec < String >` but is a `integer` 3 | 4 | fn main() {} 5 | -------------------------------------------------------------------------------- /tests/ui-toml/bad_toml_type/conf_bad_type.stderr: -------------------------------------------------------------------------------- 1 | error: error reading Clippy's configuration file `$DIR/clippy.toml`: invalid type: integer `42`, expected a sequence 2 | 3 | error: aborting due to previous error 4 | 5 | -------------------------------------------------------------------------------- /tests/ui-toml/conf_deprecated_key/clippy.toml: -------------------------------------------------------------------------------- 1 | # that one is an error 2 | cyclomatic-complexity-threshold = 42 3 | 4 | # that one is white-listed 5 | [third-party] 6 | clippy-feature = "nightly" 7 | -------------------------------------------------------------------------------- /tests/ui-toml/conf_deprecated_key/conf_deprecated_key.rs: -------------------------------------------------------------------------------- 1 | // error-pattern: error reading Clippy's configuration file: found deprecated field 2 | // `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead. 3 | 4 | fn main() {} 5 | -------------------------------------------------------------------------------- /tests/ui-toml/conf_deprecated_key/conf_deprecated_key.stderr: -------------------------------------------------------------------------------- 1 | error: error reading Clippy's configuration file `$DIR/clippy.toml`: found deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead. 2 | 3 | error: aborting due to previous error 4 | 5 | -------------------------------------------------------------------------------- /tests/ui-toml/functions_maxlines/clippy.toml: -------------------------------------------------------------------------------- 1 | too-many-lines-threshold = 1 2 | -------------------------------------------------------------------------------- /tests/ui-toml/functions_maxlines/test.stderr: -------------------------------------------------------------------------------- 1 | error: This function has a large number of lines. 2 | --> $DIR/test.rs:18:1 3 | | 4 | LL | / fn too_many_lines() { 5 | LL | | println!("This is bad."); 6 | LL | | println!("This is bad."); 7 | LL | | } 8 | | |_^ 9 | | 10 | = note: `-D clippy::too-many-lines` implied by `-D warnings` 11 | 12 | error: This function has a large number of lines. 13 | --> $DIR/test.rs:38:1 14 | | 15 | LL | / fn comment_before_code() { 16 | LL | | let _ = "test"; 17 | LL | | /* This comment extends to the front of 18 | LL | | the code but this line should still count. */ let _ = 5; 19 | LL | | } 20 | | |_^ 21 | 22 | error: aborting due to 2 previous errors 23 | 24 | -------------------------------------------------------------------------------- /tests/ui-toml/good_toml_no_false_negatives/clippy.toml: -------------------------------------------------------------------------------- 1 | # that one is white-listed 2 | [third-party] 3 | clippy-feature = "nightly" 4 | -------------------------------------------------------------------------------- /tests/ui-toml/good_toml_no_false_negatives/conf_no_false_negatives.rs: -------------------------------------------------------------------------------- 1 | // error-pattern: should give absolutely no error 2 | 3 | fn main() {} 4 | -------------------------------------------------------------------------------- /tests/ui-toml/toml_blacklist/clippy.toml: -------------------------------------------------------------------------------- 1 | blacklisted-names = ["toto", "tata", "titi"] 2 | -------------------------------------------------------------------------------- /tests/ui-toml/toml_blacklist/conf_french_blacklisted_name.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | #![allow(clippy::single_match)] 3 | #![allow(unused_variables)] 4 | #![warn(clippy::blacklisted_name)] 5 | 6 | fn test(toto: ()) {} 7 | 8 | fn main() { 9 | let toto = 42; 10 | let tata = 42; 11 | let titi = 42; 12 | 13 | let tatab = 42; 14 | let tatatataic = 42; 15 | 16 | match (42, Some(1337), Some(0)) { 17 | (toto, Some(tata), titi @ Some(_)) => (), 18 | _ => (), 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/ui-toml/toml_trivially_copy/clippy.toml: -------------------------------------------------------------------------------- 1 | trivial-copy-size-limit = 2 2 | -------------------------------------------------------------------------------- /tests/ui-toml/toml_trivially_copy/test.rs: -------------------------------------------------------------------------------- 1 | #![allow(clippy::many_single_char_names)] 2 | 3 | #[derive(Copy, Clone)] 4 | struct Foo(u8); 5 | 6 | #[derive(Copy, Clone)] 7 | struct Bar(u32); 8 | 9 | fn good(a: &mut u32, b: u32, c: &Bar, d: &u32) {} 10 | 11 | fn bad(x: &u16, y: &Foo) {} 12 | 13 | fn main() { 14 | let (mut a, b, c, d, x, y) = (0, 0, Bar(0), 0, 0, Foo(0)); 15 | good(&mut a, b, &c, &d); 16 | bad(&x, &y); 17 | } 18 | -------------------------------------------------------------------------------- /tests/ui-toml/toml_trivially_copy/test.stderr: -------------------------------------------------------------------------------- 1 | error: this argument is passed by reference, but would be more efficient if passed by value 2 | --> $DIR/test.rs:11:11 3 | | 4 | LL | fn bad(x: &u16, y: &Foo) {} 5 | | ^^^^ help: consider passing by value instead: `u16` 6 | | 7 | = note: `-D clippy::trivially-copy-pass-by-ref` implied by `-D warnings` 8 | 9 | error: this argument is passed by reference, but would be more efficient if passed by value 10 | --> $DIR/test.rs:11:20 11 | | 12 | LL | fn bad(x: &u16, y: &Foo) {} 13 | | ^^^^ help: consider passing by value instead: `Foo` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui-toml/toml_unknown_key/clippy.toml: -------------------------------------------------------------------------------- 1 | # that one is an error 2 | foobar = 42 3 | 4 | # that one is white-listed 5 | [third-party] 6 | clippy-feature = "nightly" 7 | -------------------------------------------------------------------------------- /tests/ui-toml/toml_unknown_key/conf_unknown_key.rs: -------------------------------------------------------------------------------- 1 | // error-pattern: error reading Clippy's configuration file: unknown key `foobar` 2 | 3 | fn main() {} 4 | -------------------------------------------------------------------------------- /tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr: -------------------------------------------------------------------------------- 1 | error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `third-party` 2 | 3 | error: aborting due to previous error 4 | 5 | -------------------------------------------------------------------------------- /tests/ui-toml/update-all-references.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # A script to update the references for all tests. The idea is that 4 | # you do a run, which will generate files in the build directory 5 | # containing the (normalized) actual output of the compiler. You then 6 | # run this script, which will copy those files over. If you find 7 | # yourself manually editing a foo.stderr file, you're doing it wrong. 8 | # 9 | # See all `update-references.sh`, if you just want to update a single test. 10 | 11 | if [[ "$1" == "--help" || "$1" == "-h" ]]; then 12 | echo "usage: $0" 13 | fi 14 | 15 | BUILD_DIR=$PWD/target/debug/test_build_base 16 | MY_DIR=$(dirname $0) 17 | cd $MY_DIR 18 | find . -name '*.rs' | xargs ./update-references.sh $BUILD_DIR 19 | -------------------------------------------------------------------------------- /tests/ui/assertions_on_constants.rs: -------------------------------------------------------------------------------- 1 | macro_rules! assert_const { 2 | ($len:expr) => { 3 | assert!($len > 0); 4 | debug_assert!($len < 0); 5 | }; 6 | } 7 | 8 | fn main() { 9 | assert!(true); 10 | assert!(false); 11 | assert!(true, "true message"); 12 | assert!(false, "false message"); 13 | 14 | const B: bool = true; 15 | assert!(B); 16 | 17 | const C: bool = false; 18 | assert!(C); 19 | 20 | debug_assert!(true); 21 | // Don't lint this, since there is no better way for expressing "Only panic in debug mode". 22 | debug_assert!(false); // #3948 23 | assert_const!(3); 24 | assert_const!(-1); 25 | } 26 | -------------------------------------------------------------------------------- /tests/ui/assign_ops.rs: -------------------------------------------------------------------------------- 1 | #[allow(dead_code, unused_assignments)] 2 | #[warn(clippy::assign_op_pattern)] 3 | fn main() { 4 | let mut a = 5; 5 | a = a + 1; 6 | a = 1 + a; 7 | a = a - 1; 8 | a = a * 99; 9 | a = 42 * a; 10 | a = a / 2; 11 | a = a % 5; 12 | a = a & 1; 13 | a = 1 - a; 14 | a = 5 / a; 15 | a = 42 % a; 16 | a = 6 << a; 17 | let mut s = String::new(); 18 | s = s + "bla"; 19 | } 20 | -------------------------------------------------------------------------------- /tests/ui/attrs.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::inline_always, clippy::deprecated_semver)] 2 | #![allow(clippy::assertions_on_constants)] 3 | #[inline(always)] 4 | fn test_attr_lint() { 5 | assert!(true) 6 | } 7 | 8 | #[inline(always)] 9 | fn false_positive_expr() { 10 | unreachable!() 11 | } 12 | 13 | #[inline(always)] 14 | fn false_positive_stmt() { 15 | unreachable!(); 16 | } 17 | 18 | #[inline(always)] 19 | fn empty_and_false_positive_stmt() { 20 | unreachable!(); 21 | } 22 | 23 | #[deprecated(since = "forever")] 24 | pub const SOME_CONST: u8 = 42; 25 | 26 | #[deprecated(since = "1")] 27 | pub const ANOTHER_CONST: u8 = 23; 28 | 29 | #[deprecated(since = "0.1.1")] 30 | pub const YET_ANOTHER_CONST: u8 = 0; 31 | 32 | fn main() { 33 | test_attr_lint(); 34 | if false { 35 | false_positive_expr() 36 | } 37 | if false { 38 | false_positive_stmt() 39 | } 40 | if false { 41 | empty_and_false_positive_stmt() 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/ui/attrs.stderr: -------------------------------------------------------------------------------- 1 | error: you have declared `#[inline(always)]` on `test_attr_lint`. This is usually a bad idea 2 | --> $DIR/attrs.rs:3:1 3 | | 4 | LL | #[inline(always)] 5 | | ^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::inline-always` implied by `-D warnings` 8 | 9 | error: the since field must contain a semver-compliant version 10 | --> $DIR/attrs.rs:23:14 11 | | 12 | LL | #[deprecated(since = "forever")] 13 | | ^^^^^^^^^^^^^^^^^ 14 | | 15 | = note: `-D clippy::deprecated-semver` implied by `-D warnings` 16 | 17 | error: the since field must contain a semver-compliant version 18 | --> $DIR/attrs.rs:26:14 19 | | 20 | LL | #[deprecated(since = "1")] 21 | | ^^^^^^^^^^^ 22 | 23 | error: aborting due to 3 previous errors 24 | 25 | -------------------------------------------------------------------------------- /tests/ui/author.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | #[clippy::author] 3 | let x: char = 0x45 as char; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ui/author.stdout: -------------------------------------------------------------------------------- 1 | if_chain! { 2 | if let StmtKind::Local(ref local) = stmt.node; 3 | if let Some(ref init) = local.init; 4 | if let ExprKind::Cast(ref expr, ref cast_ty) = init.node; 5 | if let TyKind::Path(ref qp) = cast_ty.node; 6 | if match_qpath(qp, &["char"]); 7 | if let ExprKind::Lit(ref lit) = expr.node; 8 | if let LitKind::Int(69, _) = lit.node; 9 | if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = local.pat.node; 10 | if name.node.as_str() == "x"; 11 | then { 12 | // report your lint here 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/author/call.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | #[clippy::author] 3 | let _ = ::std::cmp::min(3, 4); 4 | } 5 | -------------------------------------------------------------------------------- /tests/ui/author/call.stdout: -------------------------------------------------------------------------------- 1 | if_chain! { 2 | if let StmtKind::Local(ref local) = stmt.node; 3 | if let Some(ref init) = local.init; 4 | if let ExprKind::Call(ref func, ref args) = init.node; 5 | if let ExprKind::Path(ref path) = func.node; 6 | if match_qpath(path, &["{{root}}", "std", "cmp", "min"]); 7 | if args.len() == 2; 8 | if let ExprKind::Lit(ref lit) = args[0].node; 9 | if let LitKind::Int(3, _) = lit.node; 10 | if let ExprKind::Lit(ref lit1) = args[1].node; 11 | if let LitKind::Int(4, _) = lit1.node; 12 | if let PatKind::Wild = local.pat.node; 13 | then { 14 | // report your lint here 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/ui/author/for_loop.rs: -------------------------------------------------------------------------------- 1 | #![feature(stmt_expr_attributes)] 2 | 3 | fn main() { 4 | #[clippy::author] 5 | for y in 0..10 { 6 | let z = y; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/ui/author/for_loop.stderr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/author/for_loop.stderr -------------------------------------------------------------------------------- /tests/ui/author/if.rs: -------------------------------------------------------------------------------- 1 | #[allow(clippy::all)] 2 | 3 | fn main() { 4 | #[clippy::author] 5 | let _ = if true { 6 | 1 == 1; 7 | } else { 8 | 2 == 2; 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /tests/ui/author/if.stderr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/author/if.stderr -------------------------------------------------------------------------------- /tests/ui/author/matches.rs: -------------------------------------------------------------------------------- 1 | #![feature(tool_attributes)] 2 | 3 | fn main() { 4 | #[clippy::author] 5 | let a = match 42 { 6 | 16 => 5, 7 | 17 => { 8 | let x = 3; 9 | x 10 | }, 11 | _ => 1, 12 | }; 13 | } 14 | -------------------------------------------------------------------------------- /tests/ui/author/matches.stderr: -------------------------------------------------------------------------------- 1 | error: returning the result of a let binding from a block. Consider returning the expression directly. 2 | --> $DIR/matches.rs:9:13 3 | | 4 | LL | x 5 | | ^ 6 | | 7 | = note: `-D clippy::let-and-return` implied by `-D warnings` 8 | note: this expression can be directly returned 9 | --> $DIR/matches.rs:8:21 10 | | 11 | LL | let x = 3; 12 | | ^ 13 | 14 | error: aborting due to previous error 15 | 16 | -------------------------------------------------------------------------------- /tests/ui/auxiliary/proc_macro_derive.rs: -------------------------------------------------------------------------------- 1 | // no-prefer-dynamic 2 | 3 | #![crate_type = "proc-macro"] 4 | #![feature(repr128, proc_macro_hygiene, proc_macro_quote)] 5 | 6 | extern crate proc_macro; 7 | 8 | use proc_macro::{quote, TokenStream}; 9 | 10 | #[proc_macro_derive(DeriveSomething)] 11 | pub fn derive(_: TokenStream) -> TokenStream { 12 | // Shound not trigger `used_underscore_binding` 13 | let _inside_derive = 1; 14 | assert_eq!(_inside_derive, _inside_derive); 15 | 16 | let output = quote! { 17 | // Should not trigger `useless_attribute` 18 | #[allow(dead_code)] 19 | extern crate clippy_lints; 20 | }; 21 | output 22 | } 23 | -------------------------------------------------------------------------------- /tests/ui/blacklisted_name.rs: -------------------------------------------------------------------------------- 1 | #![allow( 2 | dead_code, 3 | clippy::similar_names, 4 | clippy::single_match, 5 | clippy::toplevel_ref_arg, 6 | unused_mut, 7 | unused_variables 8 | )] 9 | #![warn(clippy::blacklisted_name)] 10 | 11 | fn test(foo: ()) {} 12 | 13 | fn main() { 14 | let foo = 42; 15 | let bar = 42; 16 | let baz = 42; 17 | 18 | let barb = 42; 19 | let barbaric = 42; 20 | 21 | match (42, Some(1337), Some(0)) { 22 | (foo, Some(bar), baz @ Some(_)) => (), 23 | _ => (), 24 | } 25 | } 26 | 27 | fn issue_1647(mut foo: u8) { 28 | let mut bar = 0; 29 | if let Some(mut baz) = Some(42) {} 30 | } 31 | 32 | fn issue_1647_ref() { 33 | let ref bar = 0; 34 | if let Some(ref baz) = Some(42) {} 35 | } 36 | 37 | fn issue_1647_ref_mut() { 38 | let ref mut bar = 0; 39 | if let Some(ref mut baz) = Some(42) {} 40 | } 41 | -------------------------------------------------------------------------------- /tests/ui/box_vec.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all)] 2 | #![allow(clippy::boxed_local, clippy::needless_pass_by_value)] 3 | #![allow(clippy::blacklisted_name)] 4 | 5 | macro_rules! boxit { 6 | ($init:expr, $x:ty) => { 7 | let _: Box<$x> = Box::new($init); 8 | }; 9 | } 10 | 11 | fn test_macro() { 12 | boxit!(Vec::new(), Vec); 13 | } 14 | pub fn test(foo: Box>) { 15 | println!("{:?}", foo.get(0)) 16 | } 17 | 18 | pub fn test2(foo: Box)>) { 19 | // pass if #31 is fixed 20 | foo(vec![1, 2, 3]) 21 | } 22 | 23 | pub fn test_local_not_linted() { 24 | let _: Box>; 25 | } 26 | 27 | fn main() { 28 | test(Box::new(Vec::new())); 29 | test2(Box::new(|v| println!("{:?}", v))); 30 | test_macro(); 31 | test_local_not_linted(); 32 | } 33 | -------------------------------------------------------------------------------- /tests/ui/box_vec.stderr: -------------------------------------------------------------------------------- 1 | error: you seem to be trying to use `Box>`. Consider using just `Vec` 2 | --> $DIR/box_vec.rs:14:18 3 | | 4 | LL | pub fn test(foo: Box>) { 5 | | ^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::box-vec` implied by `-D warnings` 8 | = help: `Vec` is already on the heap, `Box>` makes an extra allocation. 9 | 10 | error: aborting due to previous error 11 | 12 | -------------------------------------------------------------------------------- /tests/ui/builtin-type-shadow.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::builtin_type_shadow)] 2 | #![allow(non_camel_case_types)] 3 | 4 | fn foo(a: u32) -> u32 { 5 | 42 6 | // ^ rustc's type error 7 | } 8 | 9 | fn main() {} 10 | -------------------------------------------------------------------------------- /tests/ui/builtin-type-shadow.stderr: -------------------------------------------------------------------------------- 1 | error: This generic shadows the built-in type `u32` 2 | --> $DIR/builtin-type-shadow.rs:4:8 3 | | 4 | LL | fn foo(a: u32) -> u32 { 5 | | ^^^ 6 | | 7 | = note: `-D clippy::builtin-type-shadow` implied by `-D warnings` 8 | 9 | error[E0308]: mismatched types 10 | --> $DIR/builtin-type-shadow.rs:5:5 11 | | 12 | LL | fn foo(a: u32) -> u32 { 13 | | --- expected `u32` because of return type 14 | LL | 42 15 | | ^^ expected type parameter, found integer 16 | | 17 | = note: expected type `u32` 18 | found type `{integer}` 19 | 20 | error: aborting due to 2 previous errors 21 | 22 | For more information about this error, try `rustc --explain E0308`. 23 | -------------------------------------------------------------------------------- /tests/ui/bytecount.rs: -------------------------------------------------------------------------------- 1 | #[deny(clippy::naive_bytecount)] 2 | fn main() { 3 | let x = vec![0_u8; 16]; 4 | 5 | let _ = x.iter().filter(|&&a| a == 0).count(); // naive byte count 6 | 7 | let _ = (&x[..]).iter().filter(|&a| *a == 0).count(); // naive byte count 8 | 9 | let _ = x.iter().filter(|a| **a > 0).count(); // not an equality count, OK. 10 | 11 | let _ = x.iter().map(|a| a + 1).filter(|&a| a < 15).count(); // not a slice 12 | 13 | let b = 0; 14 | 15 | let _ = x.iter().filter(|_| b > 0).count(); // woah there 16 | 17 | let _ = x.iter().filter(|_a| b == b + 1).count(); // nothing to see here, move along 18 | 19 | let _ = x.iter().filter(|a| b + 1 == **a).count(); // naive byte count 20 | 21 | let y = vec![0_u16; 3]; 22 | 23 | let _ = y.iter().filter(|&&a| a == 0).count(); // naive count, but not bytes 24 | } 25 | -------------------------------------------------------------------------------- /tests/ui/cast_alignment.rs: -------------------------------------------------------------------------------- 1 | //! Test casts for alignment issues 2 | 3 | #![feature(rustc_private)] 4 | extern crate libc; 5 | 6 | #[warn(clippy::cast_ptr_alignment)] 7 | #[allow(clippy::no_effect, clippy::unnecessary_operation, clippy::cast_lossless)] 8 | fn main() { 9 | /* These should be warned against */ 10 | 11 | // cast to more-strictly-aligned type 12 | (&1u8 as *const u8) as *const u16; 13 | (&mut 1u8 as *mut u8) as *mut u16; 14 | 15 | /* These should be ok */ 16 | 17 | // not a pointer type 18 | 1u8 as u16; 19 | // cast to less-strictly-aligned type 20 | (&1u16 as *const u16) as *const u8; 21 | (&mut 1u16 as *mut u16) as *mut u8; 22 | // For c_void, we should trust the user. See #2677 23 | (&1u32 as *const u32 as *const std::os::raw::c_void) as *const u32; 24 | (&1u32 as *const u32 as *const libc::c_void) as *const u32; 25 | } 26 | -------------------------------------------------------------------------------- /tests/ui/cast_alignment.stderr: -------------------------------------------------------------------------------- 1 | error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) 2 | --> $DIR/cast_alignment.rs:12:5 3 | | 4 | LL | (&1u8 as *const u8) as *const u16; 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::cast-ptr-alignment` implied by `-D warnings` 8 | 9 | error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) 10 | --> $DIR/cast_alignment.rs:13:5 11 | | 12 | LL | (&mut 1u8 as *mut u8) as *mut u16; 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/cast_lossless_float.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)] 4 | #![warn(clippy::cast_lossless)] 5 | 6 | fn main() { 7 | // Test clippy::cast_lossless with casts to floating-point types 8 | let x0 = 1i8; 9 | f32::from(x0); 10 | f64::from(x0); 11 | let x1 = 1u8; 12 | f32::from(x1); 13 | f64::from(x1); 14 | let x2 = 1i16; 15 | f32::from(x2); 16 | f64::from(x2); 17 | let x3 = 1u16; 18 | f32::from(x3); 19 | f64::from(x3); 20 | let x4 = 1i32; 21 | f64::from(x4); 22 | let x5 = 1u32; 23 | f64::from(x5); 24 | 25 | // Test with casts from floating-point types 26 | f64::from(1.0f32); 27 | } 28 | 29 | // The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const, 30 | // so we skip the lint if the expression is in a const fn. 31 | // See #3656 32 | const fn abc(input: f32) -> f64 { 33 | input as f64 34 | } 35 | -------------------------------------------------------------------------------- /tests/ui/cast_lossless_float.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)] 4 | #![warn(clippy::cast_lossless)] 5 | 6 | fn main() { 7 | // Test clippy::cast_lossless with casts to floating-point types 8 | let x0 = 1i8; 9 | x0 as f32; 10 | x0 as f64; 11 | let x1 = 1u8; 12 | x1 as f32; 13 | x1 as f64; 14 | let x2 = 1i16; 15 | x2 as f32; 16 | x2 as f64; 17 | let x3 = 1u16; 18 | x3 as f32; 19 | x3 as f64; 20 | let x4 = 1i32; 21 | x4 as f64; 22 | let x5 = 1u32; 23 | x5 as f64; 24 | 25 | // Test with casts from floating-point types 26 | 1.0f32 as f64; 27 | } 28 | 29 | // The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const, 30 | // so we skip the lint if the expression is in a const fn. 31 | // See #3656 32 | const fn abc(input: f32) -> f64 { 33 | input as f64 34 | } 35 | -------------------------------------------------------------------------------- /tests/ui/cast_lossless_integer.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)] 4 | #![warn(clippy::cast_lossless)] 5 | 6 | fn main() { 7 | // Test clippy::cast_lossless with casts to integer types 8 | i16::from(1i8); 9 | i32::from(1i8); 10 | i64::from(1i8); 11 | i16::from(1u8); 12 | i32::from(1u8); 13 | i64::from(1u8); 14 | u16::from(1u8); 15 | u32::from(1u8); 16 | u64::from(1u8); 17 | i32::from(1i16); 18 | i64::from(1i16); 19 | i32::from(1u16); 20 | i64::from(1u16); 21 | u32::from(1u16); 22 | u64::from(1u16); 23 | i64::from(1i32); 24 | i64::from(1u32); 25 | u64::from(1u32); 26 | 27 | // Test with an expression wrapped in parens 28 | u16::from(1u8 + 1u8); 29 | } 30 | 31 | // The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const, 32 | // so we skip the lint if the expression is in a const fn. 33 | // See #3656 34 | const fn abc(input: u16) -> u32 { 35 | input as u32 36 | } 37 | -------------------------------------------------------------------------------- /tests/ui/cast_lossless_integer.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)] 4 | #![warn(clippy::cast_lossless)] 5 | 6 | fn main() { 7 | // Test clippy::cast_lossless with casts to integer types 8 | 1i8 as i16; 9 | 1i8 as i32; 10 | 1i8 as i64; 11 | 1u8 as i16; 12 | 1u8 as i32; 13 | 1u8 as i64; 14 | 1u8 as u16; 15 | 1u8 as u32; 16 | 1u8 as u64; 17 | 1i16 as i32; 18 | 1i16 as i64; 19 | 1u16 as i32; 20 | 1u16 as i64; 21 | 1u16 as u32; 22 | 1u16 as u64; 23 | 1i32 as i64; 24 | 1u32 as i64; 25 | 1u32 as u64; 26 | 27 | // Test with an expression wrapped in parens 28 | (1u8 + 1u8) as u16; 29 | } 30 | 31 | // The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const, 32 | // so we skip the lint if the expression is in a const fn. 33 | // See #3656 34 | const fn abc(input: u16) -> u32 { 35 | input as u32 36 | } 37 | -------------------------------------------------------------------------------- /tests/ui/cast_ref_to_mut.stderr: -------------------------------------------------------------------------------- 1 | error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell 2 | --> $DIR/cast_ref_to_mut.rs:18:9 3 | | 4 | LL | (*(a as *const _ as *mut String)).push_str(" world"); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::cast-ref-to-mut` implied by `-D warnings` 8 | 9 | error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell 10 | --> $DIR/cast_ref_to_mut.rs:19:9 11 | | 12 | LL | *(a as *const _ as *mut _) = String::from("Replaced"); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 14 | 15 | error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell 16 | --> $DIR/cast_ref_to_mut.rs:20:9 17 | | 18 | LL | *(a as *const _ as *mut String) += " world"; 19 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/cast_size.rs: -------------------------------------------------------------------------------- 1 | #[warn( 2 | clippy::cast_precision_loss, 3 | clippy::cast_possible_truncation, 4 | clippy::cast_sign_loss, 5 | clippy::cast_possible_wrap, 6 | clippy::cast_lossless 7 | )] 8 | #[allow(clippy::no_effect, clippy::unnecessary_operation)] 9 | fn main() { 10 | // Casting from *size 11 | 1isize as i8; 12 | let x0 = 1isize; 13 | let x1 = 1usize; 14 | x0 as f64; 15 | x1 as f64; 16 | x0 as f32; 17 | x1 as f32; 18 | 1isize as i32; 19 | 1isize as u32; 20 | 1usize as u32; 21 | 1usize as i32; 22 | // Casting to *size 23 | 1i64 as isize; 24 | 1i64 as usize; 25 | 1u64 as isize; 26 | 1u64 as usize; 27 | 1u32 as isize; 28 | 1u32 as usize; // Should not trigger any lint 29 | 1i32 as isize; // Neither should this 30 | 1i32 as usize; 31 | // Big integer literal to float 32 | 999_999_999 as f32; 33 | 9_999_999_999_999_999usize as f64; 34 | } 35 | -------------------------------------------------------------------------------- /tests/ui/cfg_attr_rustfmt.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #![feature(stmt_expr_attributes)] 3 | 4 | #![allow(unused, clippy::no_effect)] 5 | #![warn(clippy::deprecated_cfg_attr)] 6 | 7 | // This doesn't get linted, see known problems 8 | #![cfg_attr(rustfmt, rustfmt_skip)] 9 | 10 | #[rustfmt::skip] 11 | trait Foo 12 | { 13 | fn foo( 14 | ); 15 | } 16 | 17 | fn skip_on_statements() { 18 | #[rustfmt::skip] 19 | 5+3; 20 | } 21 | 22 | #[rustfmt::skip] 23 | fn main() { 24 | foo::f(); 25 | } 26 | 27 | mod foo { 28 | #![cfg_attr(rustfmt, rustfmt_skip)] 29 | 30 | pub fn f() {} 31 | } 32 | -------------------------------------------------------------------------------- /tests/ui/cfg_attr_rustfmt.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #![feature(stmt_expr_attributes)] 3 | 4 | #![allow(unused, clippy::no_effect)] 5 | #![warn(clippy::deprecated_cfg_attr)] 6 | 7 | // This doesn't get linted, see known problems 8 | #![cfg_attr(rustfmt, rustfmt_skip)] 9 | 10 | #[rustfmt::skip] 11 | trait Foo 12 | { 13 | fn foo( 14 | ); 15 | } 16 | 17 | fn skip_on_statements() { 18 | #[cfg_attr(rustfmt, rustfmt::skip)] 19 | 5+3; 20 | } 21 | 22 | #[cfg_attr(rustfmt, rustfmt_skip)] 23 | fn main() { 24 | foo::f(); 25 | } 26 | 27 | mod foo { 28 | #![cfg_attr(rustfmt, rustfmt_skip)] 29 | 30 | pub fn f() {} 31 | } 32 | -------------------------------------------------------------------------------- /tests/ui/cfg_attr_rustfmt.stderr: -------------------------------------------------------------------------------- 1 | error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes 2 | --> $DIR/cfg_attr_rustfmt.rs:18:5 3 | | 4 | LL | #[cfg_attr(rustfmt, rustfmt::skip)] 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]` 6 | | 7 | = note: `-D clippy::deprecated-cfg-attr` implied by `-D warnings` 8 | 9 | error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes 10 | --> $DIR/cfg_attr_rustfmt.rs:22:1 11 | | 12 | LL | #[cfg_attr(rustfmt, rustfmt_skip)] 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/char_lit_as_u8.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::char_lit_as_u8)] 2 | #![allow(unused_variables)] 3 | fn main() { 4 | let c = 'a' as u8; 5 | } 6 | -------------------------------------------------------------------------------- /tests/ui/char_lit_as_u8.stderr: -------------------------------------------------------------------------------- 1 | error: casting character literal to u8. `char`s are 4 bytes wide in rust, so casting to u8 truncates them 2 | --> $DIR/char_lit_as_u8.rs:4:13 3 | | 4 | LL | let c = 'a' as u8; 5 | | ^^^^^^^^^ 6 | | 7 | = note: `-D clippy::char-lit-as-u8` implied by `-D warnings` 8 | = help: Consider using a byte literal instead: 9 | b'a' 10 | 11 | error: aborting due to previous error 12 | 13 | -------------------------------------------------------------------------------- /tests/ui/clone_on_copy_impl.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | use std::marker::PhantomData; 3 | 4 | pub struct Key { 5 | #[doc(hidden)] 6 | pub __name: &'static str, 7 | #[doc(hidden)] 8 | pub __phantom: PhantomData, 9 | } 10 | 11 | impl Copy for Key {} 12 | 13 | impl Clone for Key { 14 | fn clone(&self) -> Self { 15 | Key { 16 | __name: self.__name, 17 | __phantom: self.__phantom, 18 | } 19 | } 20 | } 21 | 22 | fn main() {} 23 | -------------------------------------------------------------------------------- /tests/ui/clone_on_copy_mut.rs: -------------------------------------------------------------------------------- 1 | pub fn dec_read_dec(i: &mut i32) -> i32 { 2 | *i -= 1; 3 | let ret = *i; 4 | *i -= 1; 5 | ret 6 | } 7 | 8 | #[allow(clippy::trivially_copy_pass_by_ref)] 9 | pub fn minus_1(i: &i32) -> i32 { 10 | dec_read_dec(&mut i.clone()) 11 | } 12 | 13 | fn main() { 14 | let mut i = 10; 15 | assert_eq!(minus_1(&i), 9); 16 | assert_eq!(i, 10); 17 | assert_eq!(dec_read_dec(&mut i), 9); 18 | assert_eq!(i, 8); 19 | } 20 | -------------------------------------------------------------------------------- /tests/ui/cmp_nan.rs: -------------------------------------------------------------------------------- 1 | #[warn(clippy::cmp_nan)] 2 | #[allow(clippy::float_cmp, clippy::no_effect, clippy::unnecessary_operation)] 3 | fn main() { 4 | let x = 5f32; 5 | x == std::f32::NAN; 6 | x != std::f32::NAN; 7 | x < std::f32::NAN; 8 | x > std::f32::NAN; 9 | x <= std::f32::NAN; 10 | x >= std::f32::NAN; 11 | 12 | let y = 0f64; 13 | y == std::f64::NAN; 14 | y != std::f64::NAN; 15 | y < std::f64::NAN; 16 | y > std::f64::NAN; 17 | y <= std::f64::NAN; 18 | y >= std::f64::NAN; 19 | } 20 | -------------------------------------------------------------------------------- /tests/ui/cmp_null.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::cmp_null)] 2 | #![allow(unused_mut)] 3 | 4 | use std::ptr; 5 | 6 | fn main() { 7 | let x = 0; 8 | let p: *const usize = &x; 9 | if p == ptr::null() { 10 | println!("This is surprising!"); 11 | } 12 | let mut y = 0; 13 | let mut m: *mut usize = &mut y; 14 | if m == ptr::null_mut() { 15 | println!("This is surprising, too!"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/ui/cmp_null.stderr: -------------------------------------------------------------------------------- 1 | error: Comparing with null is better expressed by the .is_null() method 2 | --> $DIR/cmp_null.rs:9:8 3 | | 4 | LL | if p == ptr::null() { 5 | | ^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::cmp-null` implied by `-D warnings` 8 | 9 | error: Comparing with null is better expressed by the .is_null() method 10 | --> $DIR/cmp_null.rs:14:8 11 | | 12 | LL | if m == ptr::null_mut() { 13 | | ^^^^^^^^^^^^^^^^^^^^ 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/cognitive_complexity_attr_used.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::cognitive_complexity)] 2 | #![warn(unused)] 3 | 4 | fn main() { 5 | kaboom(); 6 | } 7 | 8 | #[clippy::cognitive_complexity = "0"] 9 | fn kaboom() { 10 | if 42 == 43 { 11 | panic!(); 12 | } else if "cake" == "lie" { 13 | println!("what?"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/cognitive_complexity_attr_used.stderr: -------------------------------------------------------------------------------- 1 | error: the function has a cognitive complexity of 3 2 | --> $DIR/cognitive_complexity_attr_used.rs:9:1 3 | | 4 | LL | / fn kaboom() { 5 | LL | | if 42 == 43 { 6 | LL | | panic!(); 7 | LL | | } else if "cake" == "lie" { 8 | LL | | println!("what?"); 9 | LL | | } 10 | LL | | } 11 | | |_^ 12 | | 13 | = note: `-D clippy::cognitive-complexity` implied by `-D warnings` 14 | = help: you could split it up into multiple smaller functions 15 | 16 | error: aborting due to previous error 17 | 18 | -------------------------------------------------------------------------------- /tests/ui/copy_iterator.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::copy_iterator)] 2 | 3 | #[derive(Copy, Clone)] 4 | struct Countdown(u8); 5 | 6 | impl Iterator for Countdown { 7 | type Item = u8; 8 | 9 | fn next(&mut self) -> Option { 10 | self.0.checked_sub(1).map(|c| { 11 | self.0 = c; 12 | c 13 | }) 14 | } 15 | } 16 | 17 | fn main() { 18 | let my_iterator = Countdown(5); 19 | let a: Vec<_> = my_iterator.take(1).collect(); 20 | assert_eq!(a.len(), 1); 21 | let b: Vec<_> = my_iterator.collect(); 22 | assert_eq!(b.len(), 5); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/copy_iterator.stderr: -------------------------------------------------------------------------------- 1 | error: you are implementing `Iterator` on a `Copy` type 2 | --> $DIR/copy_iterator.rs:6:1 3 | | 4 | LL | / impl Iterator for Countdown { 5 | LL | | type Item = u8; 6 | LL | | 7 | LL | | fn next(&mut self) -> Option { 8 | ... | 9 | LL | | } 10 | LL | | } 11 | | |_^ 12 | | 13 | = note: `-D clippy::copy-iterator` implied by `-D warnings` 14 | = note: consider implementing `IntoIterator` instead 15 | 16 | error: aborting due to previous error 17 | 18 | -------------------------------------------------------------------------------- /tests/ui/crashes/associated-constant-ice.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | /// Test for https://github.com/rust-lang/rust-clippy/issues/1698 4 | 5 | pub trait Trait { 6 | const CONSTANT: u8; 7 | } 8 | 9 | impl Trait for u8 { 10 | const CONSTANT: u8 = 2; 11 | } 12 | 13 | fn main() { 14 | println!("{}", u8::CONSTANT * 10); 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/crashes/cc_seme.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #[allow(dead_code)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/478 6 | 7 | enum Baz { 8 | One, 9 | Two, 10 | } 11 | 12 | struct Test { 13 | t: Option, 14 | b: Baz, 15 | } 16 | 17 | fn main() {} 18 | 19 | pub fn foo() { 20 | use Baz::*; 21 | let x = Test { t: Some(0), b: One }; 22 | 23 | match x { 24 | Test { t: Some(_), b: One } => unreachable!(), 25 | Test { t: Some(42), b: Two } => unreachable!(), 26 | Test { t: None, .. } => unreachable!(), 27 | Test { .. } => unreachable!(), 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/ui/crashes/enum-glob-import-crate.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![deny(clippy::all)] 4 | #![allow(unused_imports)] 5 | 6 | use std::*; 7 | 8 | fn main() {} 9 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-1588.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(clippy::all)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/1588 6 | 7 | fn main() { 8 | match 1 { 9 | 1 => {}, 10 | 2 => { 11 | [0; 1]; 12 | }, 13 | _ => {}, 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-1782.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(dead_code, unused_variables)] 4 | 5 | /// Should not trigger an ICE in `SpanlessEq` / `consts::constant` 6 | /// 7 | /// Issue: https://github.com/rust-lang/rust-clippy/issues/1782 8 | use std::{mem, ptr}; 9 | 10 | fn spanless_eq_ice() { 11 | let txt = "something"; 12 | match txt { 13 | "something" => unsafe { 14 | ptr::write( 15 | ptr::null_mut() as *mut u32, 16 | mem::transmute::<[u8; 4], _>([0, 0, 0, 255]), 17 | ) 18 | }, 19 | _ => unsafe { 20 | ptr::write( 21 | ptr::null_mut() as *mut u32, 22 | mem::transmute::<[u8; 4], _>([13, 246, 24, 255]), 23 | ) 24 | }, 25 | } 26 | } 27 | 28 | fn main() {} 29 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-1969.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(clippy::all)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/1969 6 | 7 | fn main() {} 8 | 9 | pub trait Convert { 10 | type Action: From<*const f64>; 11 | 12 | fn convert(val: *const f64) -> Self::Action { 13 | val.into() 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-2499.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(dead_code, clippy::char_lit_as_u8, clippy::needless_bool)] 4 | 5 | /// Should not trigger an ICE in `SpanlessHash` / `consts::constant` 6 | /// 7 | /// Issue: https://github.com/rust-lang/rust-clippy/issues/2499 8 | 9 | fn f(s: &[u8]) -> bool { 10 | let t = s[0] as char; 11 | 12 | match t { 13 | 'E' | 'W' => {}, 14 | 'T' => { 15 | if s[0..4] != ['0' as u8; 4] { 16 | return false; 17 | } else { 18 | return true; 19 | } 20 | }, 21 | _ => { 22 | return false; 23 | }, 24 | } 25 | true 26 | } 27 | 28 | fn main() {} 29 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-2594.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(dead_code, unused_variables)] 4 | 5 | /// Should not trigger an ICE in `SpanlessHash` / `consts::constant` 6 | /// 7 | /// Issue: https://github.com/rust-lang/rust-clippy/issues/2594 8 | 9 | fn spanless_hash_ice() { 10 | let txt = "something"; 11 | let empty_header: [u8; 1] = [1; 1]; 12 | 13 | match txt { 14 | "something" => { 15 | let mut headers = [empty_header; 1]; 16 | }, 17 | "" => (), 18 | _ => (), 19 | } 20 | } 21 | 22 | fn main() {} 23 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-2727.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | /// Test for https://github.com/rust-lang/rust-clippy/issues/2727 4 | 5 | pub fn f(new: fn()) { 6 | new(); 7 | } 8 | 9 | fn main() {} 10 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-2760.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow( 4 | unused_variables, 5 | clippy::blacklisted_name, 6 | clippy::needless_pass_by_value, 7 | dead_code 8 | )] 9 | 10 | /// This should not compile-fail with: 11 | /// 12 | /// error[E0277]: the trait bound `T: Foo` is not satisfied 13 | // See rust-lang/rust-clippy#2760. 14 | 15 | trait Foo { 16 | type Bar; 17 | } 18 | 19 | struct Baz { 20 | bar: T::Bar, 21 | } 22 | 23 | fn take(baz: Baz) {} 24 | 25 | fn main() {} 26 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-2774.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | use std::collections::HashSet; 4 | 5 | // See rust-lang/rust-clippy#2774. 6 | 7 | #[derive(Eq, PartialEq, Debug, Hash)] 8 | pub struct Bar { 9 | foo: Foo, 10 | } 11 | 12 | #[derive(Eq, PartialEq, Debug, Hash)] 13 | pub struct Foo {} 14 | 15 | #[allow(clippy::implicit_hasher)] 16 | // This should not cause a "cannot relate bound region" ICE. 17 | pub fn add_barfoos_to_foos<'a>(bars: &HashSet<&'a Bar>) { 18 | let mut foos = HashSet::new(); 19 | foos.extend(bars.iter().map(|b| &b.foo)); 20 | } 21 | 22 | #[allow(clippy::implicit_hasher)] 23 | // Also, this should not cause a "cannot relate bound region" ICE. 24 | pub fn add_barfoos_to_foos2(bars: &HashSet<&Bar>) { 25 | let mut foos = HashSet::new(); 26 | foos.extend(bars.iter().map(|b| &b.foo)); 27 | } 28 | 29 | fn main() {} 30 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-2865.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #[allow(dead_code)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/2865 6 | 7 | struct Ice { 8 | size: String, 9 | } 10 | 11 | impl<'a> From for Ice { 12 | fn from(_: String) -> Self { 13 | let text = || "iceberg".to_string(); 14 | Self { size: text() } 15 | } 16 | } 17 | 18 | fn main() {} 19 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-3151.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | /// Test for https://github.com/rust-lang/rust-clippy/issues/2865 4 | 5 | #[derive(Clone)] 6 | pub struct HashMap { 7 | hash_builder: S, 8 | table: RawTable, 9 | } 10 | 11 | #[derive(Clone)] 12 | pub struct RawTable { 13 | size: usize, 14 | val: V, 15 | } 16 | 17 | fn main() {} 18 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-3462.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![warn(clippy::all)] 4 | #![allow(clippy::blacklisted_name)] 5 | #![allow(unused)] 6 | 7 | /// Test for https://github.com/rust-lang/rust-clippy/issues/3462 8 | 9 | enum Foo { 10 | Bar, 11 | Baz, 12 | } 13 | 14 | fn bar(foo: Foo) { 15 | macro_rules! baz { 16 | () => { 17 | if let Foo::Bar = foo {} 18 | }; 19 | } 20 | 21 | baz!(); 22 | baz!(); 23 | } 24 | 25 | fn main() {} 26 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-3741.rs: -------------------------------------------------------------------------------- 1 | // aux-build:proc_macro_crash.rs 2 | // run-pass 3 | 4 | #![feature(proc_macro_hygiene)] 5 | #![warn(clippy::suspicious_else_formatting)] 6 | 7 | extern crate proc_macro_crash; 8 | use proc_macro_crash::macro_test; 9 | 10 | fn main() { 11 | macro_test!(2); 12 | } 13 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-3747.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | /// Test for https://github.com/rust-lang/rust-clippy/issues/3747 4 | 5 | macro_rules! a { 6 | ( $pub:tt $($attr:tt)* ) => { 7 | $($attr)* $pub fn say_hello() {} 8 | }; 9 | } 10 | 11 | macro_rules! b { 12 | () => { 13 | a! { pub } 14 | }; 15 | } 16 | 17 | b! {} 18 | 19 | fn main() {} 20 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-3891.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | 1x; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-3891.stderr: -------------------------------------------------------------------------------- 1 | error: invalid suffix `x` for numeric literal 2 | --> $DIR/ice-3891.rs:2:5 3 | | 4 | LL | 1x; 5 | | ^^ invalid suffix `x` 6 | | 7 | = help: the suffix must be one of the integral types (`u32`, `isize`, etc) 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice-700.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![deny(clippy::all)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/700 6 | 7 | fn core() {} 8 | 9 | fn main() { 10 | core(); 11 | } 12 | -------------------------------------------------------------------------------- /tests/ui/crashes/ice_exacte_size.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![deny(clippy::all)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/1336 6 | 7 | #[allow(dead_code)] 8 | struct Foo; 9 | 10 | impl Iterator for Foo { 11 | type Item = (); 12 | 13 | fn next(&mut self) -> Option<()> { 14 | let _ = self.len() == 0; 15 | unimplemented!() 16 | } 17 | } 18 | 19 | impl ExactSizeIterator for Foo {} 20 | 21 | fn main() {} 22 | -------------------------------------------------------------------------------- /tests/ui/crashes/if_same_then_else.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![deny(clippy::if_same_then_else)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/2426 6 | 7 | fn main() {} 8 | 9 | pub fn foo(a: i32, b: i32) -> Option<&'static str> { 10 | if a == b { 11 | None 12 | } else if a > b { 13 | Some("a pfeil b") 14 | } else { 15 | None 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/ui/crashes/issue-2862.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | /// Test for https://github.com/rust-lang/rust-clippy/issues/2826 4 | 5 | pub trait FooMap { 6 | fn map B>(&self, f: F) -> B; 7 | } 8 | 9 | impl FooMap for bool { 10 | fn map B>(&self, f: F) -> B { 11 | f() 12 | } 13 | } 14 | 15 | fn main() { 16 | let a = true; 17 | a.map(|| false); 18 | } 19 | -------------------------------------------------------------------------------- /tests/ui/crashes/issue-825.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(warnings)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/825 6 | 7 | // this should compile in a reasonable amount of time 8 | fn rust_type_id(name: &str) { 9 | if "bool" == &name[..] 10 | || "uint" == &name[..] 11 | || "u8" == &name[..] 12 | || "u16" == &name[..] 13 | || "u32" == &name[..] 14 | || "f32" == &name[..] 15 | || "f64" == &name[..] 16 | || "i8" == &name[..] 17 | || "i16" == &name[..] 18 | || "i32" == &name[..] 19 | || "i64" == &name[..] 20 | || "Self" == &name[..] 21 | || "str" == &name[..] 22 | { 23 | unreachable!(); 24 | } 25 | } 26 | 27 | fn main() {} 28 | -------------------------------------------------------------------------------- /tests/ui/crashes/issues_loop_mut_cond.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(dead_code)] 4 | 5 | /// Issue: https://github.com/rust-lang/rust-clippy/issues/2596 6 | pub fn loop_on_block_condition(u: &mut isize) { 7 | while { *u < 0 } { 8 | *u += 1; 9 | } 10 | } 11 | 12 | /// https://github.com/rust-lang/rust-clippy/issues/2584 13 | fn loop_with_unsafe_condition(ptr: *const u8) { 14 | let mut len = 0; 15 | while unsafe { *ptr.offset(len) } != 0 { 16 | len += 1; 17 | } 18 | } 19 | 20 | /// https://github.com/rust-lang/rust-clippy/issues/2710 21 | static mut RUNNING: bool = true; 22 | fn loop_on_static_condition() { 23 | unsafe { 24 | while RUNNING { 25 | RUNNING = false; 26 | } 27 | } 28 | } 29 | 30 | fn main() {} 31 | -------------------------------------------------------------------------------- /tests/ui/crashes/match_same_arms_const.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![deny(clippy::match_same_arms)] 4 | 5 | /// Test for https://github.com/rust-lang/rust-clippy/issues/2427 6 | 7 | const PRICE_OF_SWEETS: u32 = 5; 8 | const PRICE_OF_KINDNESS: u32 = 0; 9 | const PRICE_OF_DRINKS: u32 = 5; 10 | 11 | pub fn price(thing: &str) -> u32 { 12 | match thing { 13 | "rolo" => PRICE_OF_SWEETS, 14 | "advice" => PRICE_OF_KINDNESS, 15 | "juice" => PRICE_OF_DRINKS, 16 | _ => panic!(), 17 | } 18 | } 19 | 20 | fn main() {} 21 | -------------------------------------------------------------------------------- /tests/ui/crashes/needless_borrow_fp.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #[deny(clippy::all)] 4 | #[derive(Debug)] 5 | pub enum Error { 6 | Type(&'static str), 7 | } 8 | 9 | fn main() {} 10 | -------------------------------------------------------------------------------- /tests/ui/crashes/needless_lifetimes_impl_trait.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![deny(clippy::needless_lifetimes)] 4 | #![allow(dead_code)] 5 | 6 | trait Foo {} 7 | 8 | struct Bar {} 9 | 10 | struct Baz<'a> { 11 | bar: &'a Bar, 12 | } 13 | 14 | impl<'a> Foo for Baz<'a> {} 15 | 16 | impl Bar { 17 | fn baz<'a>(&'a self) -> impl Foo + 'a { 18 | Baz { bar: self } 19 | } 20 | } 21 | 22 | fn main() {} 23 | -------------------------------------------------------------------------------- /tests/ui/crashes/procedural_macro.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #[macro_use] 4 | extern crate clippy_mini_macro_test; 5 | 6 | #[deny(warnings)] 7 | fn main() { 8 | let x = Foo; 9 | println!("{:?}", x); 10 | } 11 | 12 | #[derive(ClippyMiniMacroTest, Debug)] 13 | struct Foo; 14 | -------------------------------------------------------------------------------- /tests/ui/crashes/regressions.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(clippy::blacklisted_name)] 4 | 5 | pub fn foo(bar: *const u8) { 6 | println!("{:#p}", bar); 7 | } 8 | 9 | fn main() {} 10 | -------------------------------------------------------------------------------- /tests/ui/crashes/returns.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | /// Test for https://github.com/rust-lang/rust-clippy/issues/1346 4 | 5 | #[deny(warnings)] 6 | fn cfg_return() -> i32 { 7 | #[cfg(unix)] 8 | return 1; 9 | #[cfg(not(unix))] 10 | return 2; 11 | } 12 | 13 | #[deny(warnings)] 14 | fn cfg_let_and_return() -> i32 { 15 | #[cfg(unix)] 16 | let x = 1; 17 | #[cfg(not(unix))] 18 | let x = 2; 19 | x 20 | } 21 | 22 | fn main() { 23 | cfg_return(); 24 | cfg_let_and_return(); 25 | } 26 | -------------------------------------------------------------------------------- /tests/ui/crashes/single-match-else.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![warn(clippy::single_match_else)] 4 | 5 | //! Test for https://github.com/rust-lang/rust-clippy/issues/1588 6 | 7 | fn main() { 8 | let n = match (42, 43) { 9 | (42, n) => n, 10 | _ => panic!("typeck error"), 11 | }; 12 | assert_eq!(n, 43); 13 | } 14 | -------------------------------------------------------------------------------- /tests/ui/crashes/used_underscore_binding_macro.rs: -------------------------------------------------------------------------------- 1 | // run-pass 2 | 3 | #![allow(clippy::useless_attribute)] //issue #2910 4 | 5 | #[macro_use] 6 | extern crate serde_derive; 7 | 8 | /// Tests that we do not lint for unused underscores in a `MacroAttribute` 9 | /// expansion 10 | #[deny(clippy::used_underscore_binding)] 11 | #[derive(Deserialize)] 12 | struct MacroAttributesTest { 13 | _foo: u32, 14 | } 15 | 16 | #[test] 17 | fn macro_attributes_test() { 18 | let _ = MacroAttributesTest { _foo: 0 }; 19 | } 20 | 21 | fn main() {} 22 | -------------------------------------------------------------------------------- /tests/ui/crashes/whitelist/clippy.toml: -------------------------------------------------------------------------------- 1 | # this is ignored by Clippy, but allowed for other tools like clippy-service 2 | [third-party] 3 | clippy-feature = "nightly" 4 | -------------------------------------------------------------------------------- /tests/ui/crashes/whitelist/conf_whitelisted.rs: -------------------------------------------------------------------------------- 1 | fn main() {} 2 | -------------------------------------------------------------------------------- /tests/ui/cstring.rs: -------------------------------------------------------------------------------- 1 | fn main() {} 2 | 3 | #[allow(clippy::result_unwrap_used)] 4 | fn temporary_cstring() { 5 | use std::ffi::CString; 6 | 7 | CString::new("foo").unwrap().as_ptr(); 8 | } 9 | -------------------------------------------------------------------------------- /tests/ui/cstring.stderr: -------------------------------------------------------------------------------- 1 | error: you are getting the inner pointer of a temporary `CString` 2 | --> $DIR/cstring.rs:7:5 3 | | 4 | LL | CString::new("foo").unwrap().as_ptr(); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: #[deny(clippy::temporary_cstring_as_ptr)] on by default 8 | = note: that pointer will be invalid outside this expression 9 | help: assign the `CString` to a variable to extend its lifetime 10 | --> $DIR/cstring.rs:7:5 11 | | 12 | LL | CString::new("foo").unwrap().as_ptr(); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 14 | 15 | error: aborting due to previous error 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/dbg_macro.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::dbg_macro)] 2 | 3 | fn foo(n: u32) -> u32 { 4 | if let Some(n) = dbg!(n.checked_sub(4)) { 5 | n 6 | } else { 7 | n 8 | } 9 | } 10 | 11 | fn factorial(n: u32) -> u32 { 12 | if dbg!(n <= 1) { 13 | dbg!(1) 14 | } else { 15 | dbg!(n * factorial(n - 1)) 16 | } 17 | } 18 | 19 | fn main() { 20 | dbg!(42); 21 | dbg!(dbg!(dbg!(42))); 22 | foo(3) + dbg!(factorial(4)); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/decimal_literal_representation.rs: -------------------------------------------------------------------------------- 1 | #[warn(clippy::decimal_literal_representation)] 2 | #[allow(unused_variables)] 3 | #[rustfmt::skip] 4 | fn main() { 5 | let good = ( // Hex: 6 | 127, // 0x7F 7 | 256, // 0x100 8 | 511, // 0x1FF 9 | 2048, // 0x800 10 | 4090, // 0xFFA 11 | 16_371, // 0x3FF3 12 | 61_683, // 0xF0F3 13 | 2_131_750_925, // 0x7F0F_F00D 14 | ); 15 | let bad = ( // Hex: 16 | 32_773, // 0x8005 17 | 65_280, // 0xFF00 18 | 2_131_750_927, // 0x7F0F_F00F 19 | 2_147_483_647, // 0x7FFF_FFFF 20 | 4_042_322_160, // 0xF0F0_F0F0 21 | ); 22 | } 23 | -------------------------------------------------------------------------------- /tests/ui/default_trait_access.stdout: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/default_trait_access.stdout -------------------------------------------------------------------------------- /tests/ui/deprecated.rs: -------------------------------------------------------------------------------- 1 | #[warn(str_to_string)] 2 | #[warn(string_to_string)] 3 | #[warn(unstable_as_slice)] 4 | #[warn(unstable_as_mut_slice)] 5 | #[warn(misaligned_transmute)] 6 | 7 | fn main() {} 8 | -------------------------------------------------------------------------------- /tests/ui/deref_addrof.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn get_number() -> usize { 4 | 10 5 | } 6 | 7 | fn get_reference(n: &usize) -> &usize { 8 | n 9 | } 10 | 11 | #[allow(clippy::many_single_char_names, clippy::double_parens)] 12 | #[allow(unused_variables, unused_parens)] 13 | #[warn(clippy::deref_addrof)] 14 | fn main() { 15 | let a = 10; 16 | let aref = &a; 17 | 18 | let b = a; 19 | 20 | let b = get_number(); 21 | 22 | let b = *get_reference(&a); 23 | 24 | let bytes: Vec = vec![1, 2, 3, 4]; 25 | let b = bytes[1..2][0]; 26 | 27 | //This produces a suggestion of 'let b = (a);' which 28 | //will trigger the 'unused_parens' lint 29 | let b = (a); 30 | 31 | let b = a; 32 | 33 | #[rustfmt::skip] 34 | let b = a; 35 | 36 | let b = &a; 37 | 38 | let b = *aref; 39 | } 40 | -------------------------------------------------------------------------------- /tests/ui/deref_addrof.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn get_number() -> usize { 4 | 10 5 | } 6 | 7 | fn get_reference(n: &usize) -> &usize { 8 | n 9 | } 10 | 11 | #[allow(clippy::many_single_char_names, clippy::double_parens)] 12 | #[allow(unused_variables, unused_parens)] 13 | #[warn(clippy::deref_addrof)] 14 | fn main() { 15 | let a = 10; 16 | let aref = &a; 17 | 18 | let b = *&a; 19 | 20 | let b = *&get_number(); 21 | 22 | let b = *get_reference(&a); 23 | 24 | let bytes: Vec = vec![1, 2, 3, 4]; 25 | let b = *&bytes[1..2][0]; 26 | 27 | //This produces a suggestion of 'let b = (a);' which 28 | //will trigger the 'unused_parens' lint 29 | let b = *&(a); 30 | 31 | let b = *(&a); 32 | 33 | #[rustfmt::skip] 34 | let b = *((&a)); 35 | 36 | let b = *&&a; 37 | 38 | let b = **&aref; 39 | } 40 | -------------------------------------------------------------------------------- /tests/ui/deref_addrof_double_trigger.rs: -------------------------------------------------------------------------------- 1 | #[warn(clippy::deref_addrof)] 2 | #[allow(unused_variables)] 3 | fn main() { 4 | let a = 10; 5 | 6 | //This produces a suggestion of 'let b = *&a;' which 7 | //will trigger the 'clippy::deref_addrof' lint again 8 | let b = **&&a; 9 | 10 | { 11 | let mut x = 10; 12 | let y = *&mut x; 13 | } 14 | 15 | { 16 | //This produces a suggestion of 'let y = *&mut x' which 17 | //will trigger the 'clippy::deref_addrof' lint again 18 | let mut x = 10; 19 | let y = **&mut &mut x; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/ui/deref_addrof_double_trigger.stderr: -------------------------------------------------------------------------------- 1 | error: immediately dereferencing a reference 2 | --> $DIR/deref_addrof_double_trigger.rs:8:14 3 | | 4 | LL | let b = **&&a; 5 | | ^^^^ help: try this: `&a` 6 | | 7 | = note: `-D clippy::deref-addrof` implied by `-D warnings` 8 | 9 | error: immediately dereferencing a reference 10 | --> $DIR/deref_addrof_double_trigger.rs:12:17 11 | | 12 | LL | let y = *&mut x; 13 | | ^^^^^^^ help: try this: `x` 14 | 15 | error: immediately dereferencing a reference 16 | --> $DIR/deref_addrof_double_trigger.rs:19:18 17 | | 18 | LL | let y = **&mut &mut x; 19 | | ^^^^^^^^^^^^ help: try this: `&mut x` 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/dlist.rs: -------------------------------------------------------------------------------- 1 | #![feature(associated_type_defaults)] 2 | #![warn(clippy::linkedlist)] 3 | #![allow(dead_code, clippy::needless_pass_by_value)] 4 | 5 | extern crate alloc; 6 | use alloc::collections::linked_list::LinkedList; 7 | 8 | trait Foo { 9 | type Baz = LinkedList; 10 | fn foo(LinkedList); 11 | const BAR: Option>; 12 | } 13 | 14 | // Ok, we don’t want to warn for implementations; see issue #605. 15 | impl Foo for LinkedList { 16 | fn foo(_: LinkedList) {} 17 | const BAR: Option> = None; 18 | } 19 | 20 | struct Bar; 21 | impl Bar { 22 | fn foo(_: LinkedList) {} 23 | } 24 | 25 | pub fn test(my_favourite_linked_list: LinkedList) { 26 | println!("{:?}", my_favourite_linked_list) 27 | } 28 | 29 | pub fn test_ret() -> Option> { 30 | unimplemented!(); 31 | } 32 | 33 | pub fn test_local_not_linted() { 34 | let _: LinkedList; 35 | } 36 | 37 | fn main() { 38 | test(LinkedList::new()); 39 | test_local_not_linted(); 40 | } 41 | -------------------------------------------------------------------------------- /tests/ui/double_comparison.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn main() { 4 | let x = 1; 5 | let y = 2; 6 | if x <= y { 7 | // do something 8 | } 9 | if x <= y { 10 | // do something 11 | } 12 | if x >= y { 13 | // do something 14 | } 15 | if x >= y { 16 | // do something 17 | } 18 | if x != y { 19 | // do something 20 | } 21 | if x != y { 22 | // do something 23 | } 24 | if x == y { 25 | // do something 26 | } 27 | if x == y { 28 | // do something 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/ui/double_comparison.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn main() { 4 | let x = 1; 5 | let y = 2; 6 | if x == y || x < y { 7 | // do something 8 | } 9 | if x < y || x == y { 10 | // do something 11 | } 12 | if x == y || x > y { 13 | // do something 14 | } 15 | if x > y || x == y { 16 | // do something 17 | } 18 | if x < y || x > y { 19 | // do something 20 | } 21 | if x > y || x < y { 22 | // do something 23 | } 24 | if x <= y && x >= y { 25 | // do something 26 | } 27 | if x >= y && x <= y { 28 | // do something 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/ui/double_neg.rs: -------------------------------------------------------------------------------- 1 | #[warn(clippy::double_neg)] 2 | fn main() { 3 | let x = 1; 4 | -x; 5 | -(-x); 6 | --x; 7 | } 8 | -------------------------------------------------------------------------------- /tests/ui/double_neg.stderr: -------------------------------------------------------------------------------- 1 | error: `--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op 2 | --> $DIR/double_neg.rs:6:5 3 | | 4 | LL | --x; 5 | | ^^^ 6 | | 7 | = note: `-D clippy::double-neg` implied by `-D warnings` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/double_parens.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::double_parens)] 2 | #![allow(dead_code)] 3 | #![feature(custom_inner_attributes)] 4 | #![rustfmt::skip] 5 | 6 | fn dummy_fn(_: T) {} 7 | 8 | struct DummyStruct; 9 | 10 | impl DummyStruct { 11 | fn dummy_method(self, _: T) {} 12 | } 13 | 14 | fn simple_double_parens() -> i32 { 15 | ((0)) 16 | } 17 | 18 | fn fn_double_parens() { 19 | dummy_fn((0)); 20 | } 21 | 22 | fn method_double_parens(x: DummyStruct) { 23 | x.dummy_method((0)); 24 | } 25 | 26 | fn tuple_double_parens() -> (i32, i32) { 27 | ((1, 2)) 28 | } 29 | 30 | fn unit_double_parens() { 31 | (()) 32 | } 33 | 34 | fn fn_tuple_ok() { 35 | dummy_fn((1, 2)); 36 | } 37 | 38 | fn method_tuple_ok(x: DummyStruct) { 39 | x.dummy_method((1, 2)); 40 | } 41 | 42 | fn fn_unit_ok() { 43 | dummy_fn(()); 44 | } 45 | 46 | fn method_unit_ok(x: DummyStruct) { 47 | x.dummy_method(()); 48 | } 49 | 50 | // Issue #3206 51 | fn inside_macro() { 52 | assert_eq!((1, 2), (1, 2), "Error"); 53 | assert_eq!(((1, 2)), (1, 2), "Error"); 54 | } 55 | 56 | fn main() {} 57 | -------------------------------------------------------------------------------- /tests/ui/drop_bounds.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused)] 2 | fn foo() {} 3 | fn bar() 4 | where 5 | T: Drop, 6 | { 7 | } 8 | fn main() {} 9 | -------------------------------------------------------------------------------- /tests/ui/drop_bounds.stderr: -------------------------------------------------------------------------------- 1 | error: Bounds of the form `T: Drop` are useless. Use `std::mem::needs_drop` to detect if a type has drop glue. 2 | --> $DIR/drop_bounds.rs:2:11 3 | | 4 | LL | fn foo() {} 5 | | ^^^^ 6 | | 7 | = note: #[deny(clippy::drop_bounds)] on by default 8 | 9 | error: Bounds of the form `T: Drop` are useless. Use `std::mem::needs_drop` to detect if a type has drop glue. 10 | --> $DIR/drop_bounds.rs:5:8 11 | | 12 | LL | T: Drop, 13 | | ^^^^ 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/duplicate_underscore_argument.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::duplicate_underscore_argument)] 2 | #[allow(dead_code, unused)] 3 | 4 | fn join_the_dark_side(darth: i32, _darth: i32) {} 5 | fn join_the_light_side(knight: i32, _master: i32) {} // the Force is strong with this one 6 | 7 | fn main() { 8 | join_the_dark_side(0, 0); 9 | join_the_light_side(0, 0); 10 | } 11 | -------------------------------------------------------------------------------- /tests/ui/duplicate_underscore_argument.stderr: -------------------------------------------------------------------------------- 1 | error: `darth` already exists, having another argument having almost the same name makes code comprehension and documentation more difficult 2 | --> $DIR/duplicate_underscore_argument.rs:4:23 3 | | 4 | LL | fn join_the_dark_side(darth: i32, _darth: i32) {} 5 | | ^^^^^ 6 | | 7 | = note: `-D clippy::duplicate-underscore-argument` implied by `-D warnings` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/duration_subsec.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #![allow(dead_code)] 3 | #![warn(clippy::duration_subsec)] 4 | 5 | use std::time::Duration; 6 | 7 | fn main() { 8 | let dur = Duration::new(5, 0); 9 | 10 | let bad_millis_1 = dur.subsec_millis(); 11 | let bad_millis_2 = dur.subsec_millis(); 12 | let good_millis = dur.subsec_millis(); 13 | assert_eq!(bad_millis_1, good_millis); 14 | assert_eq!(bad_millis_2, good_millis); 15 | 16 | let bad_micros = dur.subsec_micros(); 17 | let good_micros = dur.subsec_micros(); 18 | assert_eq!(bad_micros, good_micros); 19 | 20 | // Handle refs 21 | let _ = (&dur).subsec_micros(); 22 | 23 | // Handle constants 24 | const NANOS_IN_MICRO: u32 = 1_000; 25 | let _ = dur.subsec_micros(); 26 | 27 | // Other literals aren't linted 28 | let _ = dur.subsec_nanos() / 699; 29 | } 30 | -------------------------------------------------------------------------------- /tests/ui/duration_subsec.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #![allow(dead_code)] 3 | #![warn(clippy::duration_subsec)] 4 | 5 | use std::time::Duration; 6 | 7 | fn main() { 8 | let dur = Duration::new(5, 0); 9 | 10 | let bad_millis_1 = dur.subsec_micros() / 1_000; 11 | let bad_millis_2 = dur.subsec_nanos() / 1_000_000; 12 | let good_millis = dur.subsec_millis(); 13 | assert_eq!(bad_millis_1, good_millis); 14 | assert_eq!(bad_millis_2, good_millis); 15 | 16 | let bad_micros = dur.subsec_nanos() / 1_000; 17 | let good_micros = dur.subsec_micros(); 18 | assert_eq!(bad_micros, good_micros); 19 | 20 | // Handle refs 21 | let _ = (&dur).subsec_nanos() / 1_000; 22 | 23 | // Handle constants 24 | const NANOS_IN_MICRO: u32 = 1_000; 25 | let _ = dur.subsec_nanos() / NANOS_IN_MICRO; 26 | 27 | // Other literals aren't linted 28 | let _ = dur.subsec_nanos() / 699; 29 | } 30 | -------------------------------------------------------------------------------- /tests/ui/duration_subsec.stdout: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/duration_subsec.stdout -------------------------------------------------------------------------------- /tests/ui/else_if_without_else.stderr: -------------------------------------------------------------------------------- 1 | error: if expression with an `else if`, but without a final `else` 2 | --> $DIR/else_if_without_else.rs:45:12 3 | | 4 | LL | } else if bla2() { 5 | | ____________^ 6 | LL | | //~ ERROR else if without else 7 | LL | | println!("else if"); 8 | LL | | } 9 | | |_____^ 10 | | 11 | = note: `-D clippy::else-if-without-else` implied by `-D warnings` 12 | = help: add an `else` block here 13 | 14 | error: if expression with an `else if`, but without a final `else` 15 | --> $DIR/else_if_without_else.rs:54:12 16 | | 17 | LL | } else if bla3() { 18 | | ____________^ 19 | LL | | //~ ERROR else if without else 20 | LL | | println!("else if 2"); 21 | LL | | } 22 | | |_____^ 23 | | 24 | = help: add an `else` block here 25 | 26 | error: aborting due to 2 previous errors 27 | 28 | -------------------------------------------------------------------------------- /tests/ui/empty_enum.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | #![warn(clippy::empty_enum)] 3 | 4 | enum Empty {} 5 | 6 | fn main() {} 7 | -------------------------------------------------------------------------------- /tests/ui/empty_enum.stderr: -------------------------------------------------------------------------------- 1 | error: enum with no variants 2 | --> $DIR/empty_enum.rs:4:1 3 | | 4 | LL | enum Empty {} 5 | | ^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::empty-enum` implied by `-D warnings` 8 | help: consider using the uninhabited type `!` or a wrapper around it 9 | --> $DIR/empty_enum.rs:4:1 10 | | 11 | LL | enum Empty {} 12 | | ^^^^^^^^^^^^^ 13 | 14 | error: aborting due to previous error 15 | 16 | -------------------------------------------------------------------------------- /tests/ui/enum_glob_use.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all, clippy::pedantic)] 2 | #![allow(unused_imports, dead_code, clippy::missing_docs_in_private_items)] 3 | 4 | use std::cmp::Ordering::*; 5 | 6 | enum Enum { 7 | _Foo, 8 | } 9 | 10 | use self::Enum::*; 11 | 12 | fn blarg() { 13 | use self::Enum::*; // ok, just for a function 14 | } 15 | 16 | mod blurg { 17 | pub use std::cmp::Ordering::*; // ok, re-export 18 | } 19 | 20 | mod tests { 21 | use super::*; 22 | } 23 | 24 | #[allow(non_snake_case)] 25 | mod CamelCaseName {} 26 | 27 | use CamelCaseName::*; 28 | 29 | fn main() {} 30 | -------------------------------------------------------------------------------- /tests/ui/enum_glob_use.stderr: -------------------------------------------------------------------------------- 1 | error: don't use glob imports for enum variants 2 | --> $DIR/enum_glob_use.rs:4:1 3 | | 4 | LL | use std::cmp::Ordering::*; 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::enum-glob-use` implied by `-D warnings` 8 | 9 | error: don't use glob imports for enum variants 10 | --> $DIR/enum_glob_use.rs:10:1 11 | | 12 | LL | use self::Enum::*; 13 | | ^^^^^^^^^^^^^^^^^^ 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/enums_clike.rs: -------------------------------------------------------------------------------- 1 | // ignore-x86 2 | 3 | #![warn(clippy::all)] 4 | #![allow(unused)] 5 | 6 | #[repr(usize)] 7 | enum NonPortable { 8 | X = 0x1_0000_0000, 9 | Y = 0, 10 | Z = 0x7FFF_FFFF, 11 | A = 0xFFFF_FFFF, 12 | } 13 | 14 | enum NonPortableNoHint { 15 | X = 0x1_0000_0000, 16 | Y = 0, 17 | Z = 0x7FFF_FFFF, 18 | A = 0xFFFF_FFFF, 19 | } 20 | 21 | #[repr(isize)] 22 | enum NonPortableSigned { 23 | X = -1, 24 | Y = 0x7FFF_FFFF, 25 | Z = 0xFFFF_FFFF, 26 | A = 0x1_0000_0000, 27 | B = std::i32::MIN as isize, 28 | C = (std::i32::MIN as isize) - 1, 29 | } 30 | 31 | enum NonPortableSignedNoHint { 32 | X = -1, 33 | Y = 0x7FFF_FFFF, 34 | Z = 0xFFFF_FFFF, 35 | A = 0x1_0000_0000, 36 | } 37 | 38 | /* 39 | FIXME: uncomment once https://github.com/rust-lang/rust/issues/31910 is fixed 40 | #[repr(usize)] 41 | enum NonPortable2 { 42 | X = Trait::Number, 43 | Y = 0, 44 | } 45 | 46 | trait Trait { 47 | const Number: usize = 0x1_0000_0000; 48 | } 49 | */ 50 | 51 | fn main() {} 52 | -------------------------------------------------------------------------------- /tests/ui/erasing_op.rs: -------------------------------------------------------------------------------- 1 | #[allow(clippy::no_effect)] 2 | #[warn(clippy::erasing_op)] 3 | fn main() { 4 | let x: u8 = 0; 5 | 6 | x * 0; 7 | 0 & x; 8 | 0 / x; 9 | } 10 | -------------------------------------------------------------------------------- /tests/ui/erasing_op.stderr: -------------------------------------------------------------------------------- 1 | error: this operation will always return zero. This is likely not the intended outcome 2 | --> $DIR/erasing_op.rs:6:5 3 | | 4 | LL | x * 0; 5 | | ^^^^^ 6 | | 7 | = note: `-D clippy::erasing-op` implied by `-D warnings` 8 | 9 | error: this operation will always return zero. This is likely not the intended outcome 10 | --> $DIR/erasing_op.rs:7:5 11 | | 12 | LL | 0 & x; 13 | | ^^^^^ 14 | 15 | error: this operation will always return zero. This is likely not the intended outcome 16 | --> $DIR/erasing_op.rs:8:5 17 | | 18 | LL | 0 / x; 19 | | ^^^^^ 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/escape_analysis.stderr: -------------------------------------------------------------------------------- 1 | error: local variable doesn't need to be boxed here 2 | --> $DIR/escape_analysis.rs:34:13 3 | | 4 | LL | fn warn_arg(x: Box) { 5 | | ^ 6 | | 7 | = note: `-D clippy::boxed-local` implied by `-D warnings` 8 | 9 | error: local variable doesn't need to be boxed here 10 | --> $DIR/escape_analysis.rs:125:12 11 | | 12 | LL | pub fn new(_needs_name: Box>) -> () {} 13 | | ^^^^^^^^^^^ 14 | 15 | error: local variable doesn't need to be boxed here 16 | --> $DIR/escape_analysis.rs:165:23 17 | | 18 | LL | fn closure_borrow(x: Box) { 19 | | ^ 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/filter_map_next.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all, clippy::pedantic)] 2 | 3 | fn main() { 4 | let a = ["1", "lol", "3", "NaN", "5"]; 5 | 6 | let element: Option = a.iter().filter_map(|s| s.parse().ok()).next(); 7 | assert_eq!(element, Some(1)); 8 | 9 | #[rustfmt::skip] 10 | let _: Option = vec![1, 2, 3, 4, 5, 6] 11 | .into_iter() 12 | .filter_map(|x| { 13 | if x == 2 { 14 | Some(x * 2) 15 | } else { 16 | None 17 | } 18 | }) 19 | .next(); 20 | } 21 | -------------------------------------------------------------------------------- /tests/ui/filter_map_next.stderr: -------------------------------------------------------------------------------- 1 | error: called `filter_map(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(p)` instead. 2 | --> $DIR/filter_map_next.rs:6:32 3 | | 4 | LL | let element: Option = a.iter().filter_map(|s| s.parse().ok()).next(); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::filter-map-next` implied by `-D warnings` 8 | = note: replace `filter_map(|s| s.parse().ok()).next()` with `find_map(|s| s.parse().ok())` 9 | 10 | error: called `filter_map(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(p)` instead. 11 | --> $DIR/filter_map_next.rs:10:26 12 | | 13 | LL | let _: Option = vec![1, 2, 3, 4, 5, 6] 14 | | __________________________^ 15 | LL | | .into_iter() 16 | LL | | .filter_map(|x| { 17 | LL | | if x == 2 { 18 | ... | 19 | LL | | }) 20 | LL | | .next(); 21 | | |_______________^ 22 | 23 | error: aborting due to 2 previous errors 24 | 25 | -------------------------------------------------------------------------------- /tests/ui/filter_methods.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all, clippy::pedantic)] 2 | #![allow(clippy::missing_docs_in_private_items)] 3 | 4 | fn main() { 5 | let _: Vec<_> = vec![5; 6].into_iter().filter(|&x| x == 0).map(|x| x * 2).collect(); 6 | 7 | let _: Vec<_> = vec![5_i8; 6] 8 | .into_iter() 9 | .filter(|&x| x == 0) 10 | .flat_map(|x| x.checked_mul(2)) 11 | .collect(); 12 | 13 | let _: Vec<_> = vec![5_i8; 6] 14 | .into_iter() 15 | .filter_map(|x| x.checked_mul(2)) 16 | .flat_map(|x| x.checked_mul(2)) 17 | .collect(); 18 | 19 | let _: Vec<_> = vec![5_i8; 6] 20 | .into_iter() 21 | .filter_map(|x| x.checked_mul(2)) 22 | .map(|x| x.checked_mul(2)) 23 | .collect(); 24 | } 25 | -------------------------------------------------------------------------------- /tests/ui/find_map.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all, clippy::pedantic)] 2 | 3 | #[derive(Debug, Copy, Clone)] 4 | enum Flavor { 5 | Chocolate, 6 | } 7 | 8 | #[derive(Debug, Copy, Clone)] 9 | enum Dessert { 10 | Banana, 11 | Pudding, 12 | Cake(Flavor), 13 | } 14 | 15 | fn main() { 16 | let desserts_of_the_week = vec![Dessert::Banana, Dessert::Cake(Flavor::Chocolate), Dessert::Pudding]; 17 | 18 | let a = ["lol", "NaN", "2", "5", "Xunda"]; 19 | 20 | let _: Option = a.iter().find(|s| s.parse::().is_ok()).map(|s| s.parse().unwrap()); 21 | 22 | let _: Option = desserts_of_the_week 23 | .iter() 24 | .find(|dessert| match *dessert { 25 | Dessert::Cake(_) => true, 26 | _ => false, 27 | }) 28 | .map(|dessert| match *dessert { 29 | Dessert::Cake(ref flavor) => *flavor, 30 | _ => unreachable!(), 31 | }); 32 | } 33 | -------------------------------------------------------------------------------- /tests/ui/find_map.stderr: -------------------------------------------------------------------------------- 1 | error: called `find(p).map(q)` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead. 2 | --> $DIR/find_map.rs:20:26 3 | | 4 | LL | let _: Option = a.iter().find(|s| s.parse::().is_ok()).map(|s| s.parse().unwrap()); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::find-map` implied by `-D warnings` 8 | 9 | error: called `find(p).map(q)` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead. 10 | --> $DIR/find_map.rs:22:29 11 | | 12 | LL | let _: Option = desserts_of_the_week 13 | | _____________________________^ 14 | LL | | .iter() 15 | LL | | .find(|dessert| match *dessert { 16 | LL | | Dessert::Cake(_) => true, 17 | ... | 18 | LL | | _ => unreachable!(), 19 | LL | | }); 20 | | |__________^ 21 | 22 | error: aborting due to 2 previous errors 23 | 24 | -------------------------------------------------------------------------------- /tests/ui/for_loop.stdout: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/for_loop.stdout -------------------------------------------------------------------------------- /tests/ui/functions_maxlines.stderr: -------------------------------------------------------------------------------- 1 | error: This function has a large number of lines. 2 | --> $DIR/functions_maxlines.rs:58:1 3 | | 4 | LL | / fn bad_lines() { 5 | LL | | println!("This is bad."); 6 | LL | | println!("This is bad."); 7 | LL | | println!("This is bad."); 8 | ... | 9 | LL | | println!("This is bad."); 10 | LL | | } 11 | | |_^ 12 | | 13 | = note: `-D clippy::too-many-lines` implied by `-D warnings` 14 | 15 | error: aborting due to previous error 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/ice-2636.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | 3 | enum Foo { 4 | A, 5 | B, 6 | C, 7 | } 8 | 9 | macro_rules! test_hash { 10 | ($foo:expr, $($t:ident => $ord:expr),+ ) => { 11 | use self::Foo::*; 12 | match $foo { 13 | $ ( & $t => $ord, 14 | )* 15 | }; 16 | }; 17 | } 18 | 19 | fn main() { 20 | let a = Foo::A; 21 | test_hash!(&a, A => 0, B => 1, C => 2); 22 | } 23 | -------------------------------------------------------------------------------- /tests/ui/ice-2636.stderr: -------------------------------------------------------------------------------- 1 | error: you don't need to add `&` to both the expression and the patterns 2 | --> $DIR/ice-2636.rs:12:9 3 | | 4 | LL | / match $foo { 5 | LL | | $ ( & $t => $ord, 6 | LL | | )* 7 | LL | | }; 8 | | |_________^ 9 | ... 10 | LL | test_hash!(&a, A => 0, B => 1, C => 2); 11 | | --------------------------------------- in this macro invocation 12 | | 13 | = note: `-D clippy::match-ref-pats` implied by `-D warnings` 14 | 15 | error: aborting due to previous error 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/ice-360.rs: -------------------------------------------------------------------------------- 1 | fn main() {} 2 | 3 | fn no_panic(slice: &[T]) { 4 | let mut iter = slice.iter(); 5 | loop { 6 | let _ = match iter.next() { 7 | Some(ele) => ele, 8 | None => break, 9 | }; 10 | loop {} 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/ui/ice-360.stderr: -------------------------------------------------------------------------------- 1 | error: this loop could be written as a `while let` loop 2 | --> $DIR/ice-360.rs:5:5 3 | | 4 | LL | / loop { 5 | LL | | let _ = match iter.next() { 6 | LL | | Some(ele) => ele, 7 | LL | | None => break, 8 | LL | | }; 9 | LL | | loop {} 10 | LL | | } 11 | | |_____^ help: try: `while let Some(ele) = iter.next() { .. }` 12 | | 13 | = note: `-D clippy::while-let-loop` implied by `-D warnings` 14 | 15 | error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body. 16 | --> $DIR/ice-360.rs:10:9 17 | | 18 | LL | loop {} 19 | | ^^^^^^^ 20 | | 21 | = note: `-D clippy::empty-loop` implied by `-D warnings` 22 | 23 | error: aborting due to 2 previous errors 24 | 25 | -------------------------------------------------------------------------------- /tests/ui/ice-3717.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashSet; 2 | 3 | fn main() {} 4 | 5 | pub fn ice_3717(_: &HashSet) { 6 | let _ = [0u8; 0]; 7 | let _: HashSet = HashSet::new(); 8 | } 9 | -------------------------------------------------------------------------------- /tests/ui/ice-3717.stderr: -------------------------------------------------------------------------------- 1 | error: parameter of type `HashSet` should be generalized over different hashers 2 | --> $DIR/ice-3717.rs:5:21 3 | | 4 | LL | pub fn ice_3717(_: &HashSet) { 5 | | ^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::implicit-hasher` implied by `-D warnings` 8 | help: consider adding a type parameter 9 | | 10 | LL | pub fn ice_3717(_: &HashSet) { 11 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ 12 | help: ...and use generic constructor 13 | | 14 | LL | let _: HashSet = HashSet::default(); 15 | | ^^^^^^^^^^^^^^^^^^ 16 | 17 | error: aborting due to previous error 18 | 19 | -------------------------------------------------------------------------------- /tests/ui/identity_op.rs: -------------------------------------------------------------------------------- 1 | const ONE: i64 = 1; 2 | const NEG_ONE: i64 = -1; 3 | const ZERO: i64 = 0; 4 | 5 | #[allow( 6 | clippy::eq_op, 7 | clippy::no_effect, 8 | clippy::unnecessary_operation, 9 | clippy::double_parens 10 | )] 11 | #[warn(clippy::identity_op)] 12 | #[rustfmt::skip] 13 | fn main() { 14 | let x = 0; 15 | 16 | x + 0; 17 | x + (1 - 1); 18 | x + 1; 19 | 0 + x; 20 | 1 + x; 21 | x - ZERO; //no error, as we skip lookups (for now) 22 | x | (0); 23 | ((ZERO)) | x; //no error, as we skip lookups (for now) 24 | 25 | x * 1; 26 | 1 * x; 27 | x / ONE; //no error, as we skip lookups (for now) 28 | 29 | x / 2; //no false positive 30 | 31 | x & NEG_ONE; //no error, as we skip lookups (for now) 32 | -1 & x; 33 | 34 | let u: u8 = 0; 35 | u & 255; 36 | } 37 | -------------------------------------------------------------------------------- /tests/ui/if_not_else.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all)] 2 | #![warn(clippy::if_not_else)] 3 | 4 | fn bla() -> bool { 5 | unimplemented!() 6 | } 7 | 8 | fn main() { 9 | if !bla() { 10 | println!("Bugs"); 11 | } else { 12 | println!("Bunny"); 13 | } 14 | if 4 != 5 { 15 | println!("Bugs"); 16 | } else { 17 | println!("Bunny"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/ui/if_not_else.stderr: -------------------------------------------------------------------------------- 1 | error: Unnecessary boolean `not` operation 2 | --> $DIR/if_not_else.rs:9:5 3 | | 4 | LL | / if !bla() { 5 | LL | | println!("Bugs"); 6 | LL | | } else { 7 | LL | | println!("Bunny"); 8 | LL | | } 9 | | |_____^ 10 | | 11 | = note: `-D clippy::if-not-else` implied by `-D warnings` 12 | = help: remove the `!` and swap the blocks of the if/else 13 | 14 | error: Unnecessary `!=` operation 15 | --> $DIR/if_not_else.rs:14:5 16 | | 17 | LL | / if 4 != 5 { 18 | LL | | println!("Bugs"); 19 | LL | | } else { 20 | LL | | println!("Bunny"); 21 | LL | | } 22 | | |_____^ 23 | | 24 | = help: change to `==` and swap the blocks of the if/else 25 | 26 | error: aborting due to 2 previous errors 27 | 28 | -------------------------------------------------------------------------------- /tests/ui/ifs_same_cond.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::ifs_same_cond)] 2 | #![allow(clippy::if_same_then_else)] // all empty blocks 3 | 4 | fn ifs_same_cond() { 5 | let a = 0; 6 | let b = false; 7 | 8 | if b { 9 | } else if b { 10 | //~ ERROR ifs same condition 11 | } 12 | 13 | if a == 1 { 14 | } else if a == 1 { 15 | //~ ERROR ifs same condition 16 | } 17 | 18 | if 2 * a == 1 { 19 | } else if 2 * a == 2 { 20 | } else if 2 * a == 1 { 21 | //~ ERROR ifs same condition 22 | } else if a == 1 { 23 | } 24 | 25 | // See #659 26 | if cfg!(feature = "feature1-659") { 27 | 1 28 | } else if cfg!(feature = "feature2-659") { 29 | 2 30 | } else { 31 | 3 32 | }; 33 | 34 | let mut v = vec![1]; 35 | if v.pop() == None { 36 | // ok, functions 37 | } else if v.pop() == None { 38 | } 39 | 40 | if v.len() == 42 { 41 | // ok, functions 42 | } else if v.len() == 42 { 43 | } 44 | } 45 | 46 | fn main() {} 47 | -------------------------------------------------------------------------------- /tests/ui/ifs_same_cond.stderr: -------------------------------------------------------------------------------- 1 | error: this `if` has the same condition as a previous if 2 | --> $DIR/ifs_same_cond.rs:9:15 3 | | 4 | LL | } else if b { 5 | | ^ 6 | | 7 | = note: `-D clippy::ifs-same-cond` implied by `-D warnings` 8 | note: same as this 9 | --> $DIR/ifs_same_cond.rs:8:8 10 | | 11 | LL | if b { 12 | | ^ 13 | 14 | error: this `if` has the same condition as a previous if 15 | --> $DIR/ifs_same_cond.rs:14:15 16 | | 17 | LL | } else if a == 1 { 18 | | ^^^^^^ 19 | | 20 | note: same as this 21 | --> $DIR/ifs_same_cond.rs:13:8 22 | | 23 | LL | if a == 1 { 24 | | ^^^^^^ 25 | 26 | error: this `if` has the same condition as a previous if 27 | --> $DIR/ifs_same_cond.rs:20:15 28 | | 29 | LL | } else if 2 * a == 1 { 30 | | ^^^^^^^^^^ 31 | | 32 | note: same as this 33 | --> $DIR/ifs_same_cond.rs:18:8 34 | | 35 | LL | if 2 * a == 1 { 36 | | ^^^^^^^^^^ 37 | 38 | error: aborting due to 3 previous errors 39 | 40 | -------------------------------------------------------------------------------- /tests/ui/impl.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | #![warn(clippy::multiple_inherent_impl)] 3 | 4 | struct MyStruct; 5 | 6 | impl MyStruct { 7 | fn first() {} 8 | } 9 | 10 | impl MyStruct { 11 | fn second() {} 12 | } 13 | 14 | impl<'a> MyStruct { 15 | fn lifetimed() {} 16 | } 17 | 18 | mod submod { 19 | struct MyStruct; 20 | impl MyStruct { 21 | fn other() {} 22 | } 23 | 24 | impl super::MyStruct { 25 | fn third() {} 26 | } 27 | } 28 | 29 | use std::fmt; 30 | impl fmt::Debug for MyStruct { 31 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 32 | write!(f, "MyStruct {{ }}") 33 | } 34 | } 35 | 36 | fn main() {} 37 | -------------------------------------------------------------------------------- /tests/ui/impl.stderr: -------------------------------------------------------------------------------- 1 | error: Multiple implementations of this structure 2 | --> $DIR/impl.rs:10:1 3 | | 4 | LL | / impl MyStruct { 5 | LL | | fn second() {} 6 | LL | | } 7 | | |_^ 8 | | 9 | = note: `-D clippy::multiple-inherent-impl` implied by `-D warnings` 10 | note: First implementation here 11 | --> $DIR/impl.rs:6:1 12 | | 13 | LL | / impl MyStruct { 14 | LL | | fn first() {} 15 | LL | | } 16 | | |_^ 17 | 18 | error: Multiple implementations of this structure 19 | --> $DIR/impl.rs:24:5 20 | | 21 | LL | / impl super::MyStruct { 22 | LL | | fn third() {} 23 | LL | | } 24 | | |_____^ 25 | | 26 | note: First implementation here 27 | --> $DIR/impl.rs:6:1 28 | | 29 | LL | / impl MyStruct { 30 | LL | | fn first() {} 31 | LL | | } 32 | | |_^ 33 | 34 | error: aborting due to 2 previous errors 35 | 36 | -------------------------------------------------------------------------------- /tests/ui/inconsistent_digit_grouping.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #[warn(clippy::inconsistent_digit_grouping)] 3 | #[allow(unused_variables, clippy::excessive_precision)] 4 | fn main() { 5 | let good = ( 6 | 123, 7 | 1_234, 8 | 1_2345_6789, 9 | 123_f32, 10 | 1_234.12_f32, 11 | 1_234.123_4_f32, 12 | 1.123_456_7_f32, 13 | ); 14 | let bad = (123_456, 12_345_678, 1_234_567, 1_234.567_8_f32, 1.234_567_8_f32); 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/inconsistent_digit_grouping.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #[warn(clippy::inconsistent_digit_grouping)] 3 | #[allow(unused_variables, clippy::excessive_precision)] 4 | fn main() { 5 | let good = ( 6 | 123, 7 | 1_234, 8 | 1_2345_6789, 9 | 123_f32, 10 | 1_234.12_f32, 11 | 1_234.123_4_f32, 12 | 1.123_456_7_f32, 13 | ); 14 | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32); 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/inline_fn_without_body.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::inline_fn_without_body)] 2 | #![allow(clippy::inline_always)] 3 | 4 | trait Foo { 5 | #[inline] 6 | fn default_inline(); 7 | 8 | #[inline(always)] 9 | fn always_inline(); 10 | 11 | #[inline(never)] 12 | fn never_inline(); 13 | 14 | #[inline] 15 | fn has_body() {} 16 | } 17 | 18 | fn main() {} 19 | -------------------------------------------------------------------------------- /tests/ui/inline_fn_without_body.stderr: -------------------------------------------------------------------------------- 1 | error: use of `#[inline]` on trait method `default_inline` which has no body 2 | --> $DIR/inline_fn_without_body.rs:5:5 3 | | 4 | LL | #[inline] 5 | | _____-^^^^^^^^ 6 | LL | | fn default_inline(); 7 | | |____- help: remove 8 | | 9 | = note: `-D clippy::inline-fn-without-body` implied by `-D warnings` 10 | 11 | error: use of `#[inline]` on trait method `always_inline` which has no body 12 | --> $DIR/inline_fn_without_body.rs:8:5 13 | | 14 | LL | #[inline(always)] 15 | | _____-^^^^^^^^^^^^^^^^ 16 | LL | | fn always_inline(); 17 | | |____- help: remove 18 | 19 | error: use of `#[inline]` on trait method `never_inline` which has no body 20 | --> $DIR/inline_fn_without_body.rs:11:5 21 | | 22 | LL | #[inline(never)] 23 | | _____-^^^^^^^^^^^^^^^ 24 | LL | | fn never_inline(); 25 | | |____- help: remove 26 | 27 | error: aborting due to 3 previous errors 28 | 29 | -------------------------------------------------------------------------------- /tests/ui/int_plus_one.rs: -------------------------------------------------------------------------------- 1 | #[allow(clippy::no_effect, clippy::unnecessary_operation)] 2 | #[warn(clippy::int_plus_one)] 3 | fn main() { 4 | let x = 1i32; 5 | let y = 0i32; 6 | 7 | x >= y + 1; 8 | y + 1 <= x; 9 | 10 | x - 1 >= y; 11 | y <= x - 1; 12 | 13 | x > y; // should be ok 14 | y < x; // should be ok 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/issue-3145.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("{}" a); //~ERROR expected token: `,` 3 | } 4 | -------------------------------------------------------------------------------- /tests/ui/issue-3145.stderr: -------------------------------------------------------------------------------- 1 | error: expected token: `,` 2 | --> $DIR/issue-3145.rs:2:19 3 | | 4 | LL | println!("{}" a); //~ERROR expected token: `,` 5 | | ^ 6 | 7 | error: aborting due to previous error 8 | 9 | -------------------------------------------------------------------------------- /tests/ui/issue_2356.rs: -------------------------------------------------------------------------------- 1 | #![deny(clippy::while_let_on_iterator)] 2 | 3 | use std::iter::Iterator; 4 | 5 | struct Foo; 6 | 7 | impl Foo { 8 | fn foo1>(mut it: I) { 9 | while let Some(_) = it.next() { 10 | println!("{:?}", it.size_hint()); 11 | } 12 | } 13 | 14 | fn foo2>(mut it: I) { 15 | while let Some(e) = it.next() { 16 | println!("{:?}", e); 17 | } 18 | } 19 | } 20 | 21 | fn main() { 22 | Foo::foo1(vec![].into_iter()); 23 | Foo::foo2(vec![].into_iter()); 24 | } 25 | -------------------------------------------------------------------------------- /tests/ui/issue_2356.stderr: -------------------------------------------------------------------------------- 1 | error: this loop could be written as a `for` loop 2 | --> $DIR/issue_2356.rs:15:29 3 | | 4 | LL | while let Some(e) = it.next() { 5 | | ^^^^^^^^^ help: try: `for e in it { .. }` 6 | | 7 | note: lint level defined here 8 | --> $DIR/issue_2356.rs:1:9 9 | | 10 | LL | #![deny(clippy::while_let_on_iterator)] 11 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 12 | 13 | error: aborting due to previous error 14 | 15 | -------------------------------------------------------------------------------- /tests/ui/issue_3849.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | #![allow(clippy::zero_ptr)] 3 | #![allow(clippy::transmute_ptr_to_ref)] 4 | #![allow(clippy::transmuting_null)] 5 | 6 | pub const ZPTR: *const usize = 0 as *const _; 7 | 8 | fn main() { 9 | unsafe { 10 | #[clippy::author] 11 | let _: &i32 = std::mem::transmute(ZPTR); 12 | let _: &i32 = std::mem::transmute(0 as *const i32); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/issue_3849.stdout: -------------------------------------------------------------------------------- 1 | if_chain! { 2 | if let StmtKind::Local(ref local) = stmt.node; 3 | if let Some(ref init) = local.init; 4 | if let ExprKind::Call(ref func, ref args) = init.node; 5 | if let ExprKind::Path(ref path) = func.node; 6 | if match_qpath(path, &["std", "mem", "transmute"]); 7 | if args.len() == 1; 8 | if let ExprKind::Path(ref path1) = args[0].node; 9 | if match_qpath(path1, &["ZPTR"]); 10 | if let PatKind::Wild = local.pat.node; 11 | then { 12 | // report your lint here 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/item_after_statement.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::items_after_statements)] 2 | 3 | fn ok() { 4 | fn foo() { 5 | println!("foo"); 6 | } 7 | foo(); 8 | } 9 | 10 | fn last() { 11 | foo(); 12 | fn foo() { 13 | println!("foo"); 14 | } 15 | } 16 | 17 | fn main() { 18 | foo(); 19 | fn foo() { 20 | println!("foo"); 21 | } 22 | foo(); 23 | } 24 | 25 | fn mac() { 26 | let mut a = 5; 27 | println!("{}", a); 28 | // do not lint this, because it needs to be after `a` 29 | macro_rules! b { 30 | () => {{ 31 | a = 6 32 | }}; 33 | } 34 | b!(); 35 | println!("{}", a); 36 | } 37 | -------------------------------------------------------------------------------- /tests/ui/item_after_statement.stderr: -------------------------------------------------------------------------------- 1 | error: adding items after statements is confusing, since items exist from the start of the scope 2 | --> $DIR/item_after_statement.rs:12:5 3 | | 4 | LL | / fn foo() { 5 | LL | | println!("foo"); 6 | LL | | } 7 | | |_____^ 8 | | 9 | = note: `-D clippy::items-after-statements` implied by `-D warnings` 10 | 11 | error: adding items after statements is confusing, since items exist from the start of the scope 12 | --> $DIR/item_after_statement.rs:19:5 13 | | 14 | LL | / fn foo() { 15 | LL | | println!("foo"); 16 | LL | | } 17 | | |_____^ 18 | 19 | error: aborting due to 2 previous errors 20 | 21 | -------------------------------------------------------------------------------- /tests/ui/iter_skip_next.rs: -------------------------------------------------------------------------------- 1 | // aux-build:option_helpers.rs 2 | 3 | #![warn(clippy::iter_skip_next)] 4 | #![allow(clippy::blacklisted_name)] 5 | 6 | extern crate option_helpers; 7 | 8 | use option_helpers::IteratorFalsePositives; 9 | 10 | /// Checks implementation of `ITER_SKIP_NEXT` lint 11 | fn iter_skip_next() { 12 | let mut some_vec = vec![0, 1, 2, 3]; 13 | let _ = some_vec.iter().skip(42).next(); 14 | let _ = some_vec.iter().cycle().skip(42).next(); 15 | let _ = (1..10).skip(10).next(); 16 | let _ = &some_vec[..].iter().skip(3).next(); 17 | let foo = IteratorFalsePositives { foo: 0 }; 18 | let _ = foo.skip(42).next(); 19 | let _ = foo.filter().skip(42).next(); 20 | } 21 | 22 | fn main() {} 23 | -------------------------------------------------------------------------------- /tests/ui/large_digit_groups.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #[warn(clippy::large_digit_groups)] 3 | #[allow(unused_variables)] 4 | fn main() { 5 | let good = ( 6 | 0b1011_i64, 7 | 0o1_234_u32, 8 | 0x1_234_567, 9 | 1_2345_6789, 10 | 1234_f32, 11 | 1_234.12_f32, 12 | 1_234.123_f32, 13 | 1.123_4_f32, 14 | ); 15 | let bad = ( 16 | 0b11_0110_i64, 17 | 0x0123_4567_8901_usize, 18 | 123_456_f32, 19 | 123_456.12_f32, 20 | 123_456.123_45_f64, 21 | 123_456.123_456_f64, 22 | ); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/large_digit_groups.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #[warn(clippy::large_digit_groups)] 3 | #[allow(unused_variables)] 4 | fn main() { 5 | let good = ( 6 | 0b1011_i64, 7 | 0o1_234_u32, 8 | 0x1_234_567, 9 | 1_2345_6789, 10 | 1234_f32, 11 | 1_234.12_f32, 12 | 1_234.123_f32, 13 | 1.123_4_f32, 14 | ); 15 | let bad = ( 16 | 0b1_10110_i64, 17 | 0x1_23456_78901_usize, 18 | 1_23456_f32, 19 | 1_23456.12_f32, 20 | 1_23456.12345_f64, 21 | 1_23456.12345_6_f64, 22 | ); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/let_return.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused)] 2 | #![warn(clippy::let_and_return)] 3 | 4 | fn test() -> i32 { 5 | let _y = 0; // no warning 6 | let x = 5; 7 | x 8 | } 9 | 10 | fn test_inner() -> i32 { 11 | if true { 12 | let x = 5; 13 | x 14 | } else { 15 | 0 16 | } 17 | } 18 | 19 | fn test_nowarn_1() -> i32 { 20 | let mut x = 5; 21 | x += 1; 22 | x 23 | } 24 | 25 | fn test_nowarn_2() -> i32 { 26 | let x = 5; 27 | x + 1 28 | } 29 | 30 | fn test_nowarn_3() -> (i32, i32) { 31 | // this should technically warn, but we do not compare complex patterns 32 | let (x, y) = (5, 9); 33 | (x, y) 34 | } 35 | 36 | fn test_nowarn_4() -> i32 { 37 | // this should technically warn, but not b/c of clippy::let_and_return, but b/c of useless type 38 | let x: i32 = 5; 39 | x 40 | } 41 | 42 | fn test_nowarn_5(x: i16) -> u16 { 43 | #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] 44 | let x = x as u16; 45 | x 46 | } 47 | 48 | fn main() {} 49 | -------------------------------------------------------------------------------- /tests/ui/let_return.stderr: -------------------------------------------------------------------------------- 1 | error: returning the result of a let binding from a block. Consider returning the expression directly. 2 | --> $DIR/let_return.rs:7:5 3 | | 4 | LL | x 5 | | ^ 6 | | 7 | = note: `-D clippy::let-and-return` implied by `-D warnings` 8 | note: this expression can be directly returned 9 | --> $DIR/let_return.rs:6:13 10 | | 11 | LL | let x = 5; 12 | | ^ 13 | 14 | error: returning the result of a let binding from a block. Consider returning the expression directly. 15 | --> $DIR/let_return.rs:13:9 16 | | 17 | LL | x 18 | | ^ 19 | | 20 | note: this expression can be directly returned 21 | --> $DIR/let_return.rs:12:17 22 | | 23 | LL | let x = 5; 24 | | ^ 25 | 26 | error: aborting due to 2 previous errors 27 | 28 | -------------------------------------------------------------------------------- /tests/ui/let_unit.stderr: -------------------------------------------------------------------------------- 1 | error: this let-binding has unit value. Consider omitting `let _x =` 2 | --> $DIR/let_unit.rs:11:5 3 | | 4 | LL | let _x = println!("x"); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::let-unit-value` implied by `-D warnings` 8 | 9 | error: this let-binding has unit value. Consider omitting `let _a =` 10 | --> $DIR/let_unit.rs:15:9 11 | | 12 | LL | let _a = (); 13 | | ^^^^^^^^^^^^ 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/lint_without_lint_pass.rs: -------------------------------------------------------------------------------- 1 | #![deny(clippy::internal)] 2 | #![feature(rustc_private)] 3 | 4 | #[macro_use] 5 | extern crate rustc; 6 | use rustc::lint::{LintArray, LintPass}; 7 | 8 | #[macro_use] 9 | extern crate clippy_lints; 10 | 11 | declare_clippy_lint! { 12 | pub TEST_LINT, 13 | correctness, 14 | "" 15 | } 16 | 17 | declare_clippy_lint! { 18 | pub TEST_LINT_REGISTERED, 19 | correctness, 20 | "" 21 | } 22 | 23 | pub struct Pass; 24 | impl LintPass for Pass { 25 | fn get_lints(&self) -> LintArray { 26 | lint_array!(TEST_LINT_REGISTERED) 27 | } 28 | 29 | fn name(&self) -> &'static str { 30 | "TEST_LINT" 31 | } 32 | } 33 | 34 | declare_lint_pass!(Pass2 => [TEST_LINT_REGISTERED]); 35 | 36 | pub struct Pass3; 37 | impl_lint_pass!(Pass3 => [TEST_LINT_REGISTERED]); 38 | 39 | fn main() {} 40 | -------------------------------------------------------------------------------- /tests/ui/lint_without_lint_pass.stderr: -------------------------------------------------------------------------------- 1 | error: the lint `TEST_LINT` is not added to any `LintPass` 2 | --> $DIR/lint_without_lint_pass.rs:11:1 3 | | 4 | LL | / declare_clippy_lint! { 5 | LL | | pub TEST_LINT, 6 | LL | | correctness, 7 | LL | | "" 8 | LL | | } 9 | | |_^ 10 | | 11 | note: lint level defined here 12 | --> $DIR/lint_without_lint_pass.rs:1:9 13 | | 14 | LL | #![deny(clippy::internal)] 15 | | ^^^^^^^^^^^^^^^^ 16 | = note: #[deny(clippy::lint_without_lint_pass)] implied by #[deny(clippy::internal)] 17 | = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) 18 | 19 | error: aborting due to previous error 20 | 21 | -------------------------------------------------------------------------------- /tests/ui/map_clone.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #![warn(clippy::all, clippy::pedantic)] 3 | #![allow(clippy::iter_cloned_collect)] 4 | #![allow(clippy::clone_on_copy)] 5 | #![allow(clippy::missing_docs_in_private_items)] 6 | #![allow(clippy::redundant_closure)] 7 | 8 | fn main() { 9 | let _: Vec = vec![5_i8; 6].iter().copied().collect(); 10 | let _: Vec = vec![String::new()].iter().cloned().collect(); 11 | let _: Vec = vec![42, 43].iter().copied().collect(); 12 | let _: Option = Some(Box::new(16)).map(|b| *b); 13 | let _: Option = Some(&16).copied(); 14 | let _: Option = Some(&1).copied(); 15 | 16 | // Don't lint these 17 | let v = vec![5_i8; 6]; 18 | let a = 0; 19 | let b = &a; 20 | let _ = v.iter().map(|_x| *b); 21 | let _ = v.iter().map(|_x| a.clone()); 22 | let _ = v.iter().map(|&_x| a); 23 | 24 | // Issue #498 25 | let _ = std::env::args(); 26 | } 27 | -------------------------------------------------------------------------------- /tests/ui/map_clone.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #![warn(clippy::all, clippy::pedantic)] 3 | #![allow(clippy::iter_cloned_collect)] 4 | #![allow(clippy::clone_on_copy)] 5 | #![allow(clippy::missing_docs_in_private_items)] 6 | #![allow(clippy::redundant_closure)] 7 | 8 | fn main() { 9 | let _: Vec = vec![5_i8; 6].iter().map(|x| *x).collect(); 10 | let _: Vec = vec![String::new()].iter().map(|x| x.clone()).collect(); 11 | let _: Vec = vec![42, 43].iter().map(|&x| x).collect(); 12 | let _: Option = Some(Box::new(16)).map(|b| *b); 13 | let _: Option = Some(&16).map(|b| *b); 14 | let _: Option = Some(&1).map(|x| x.clone()); 15 | 16 | // Don't lint these 17 | let v = vec![5_i8; 6]; 18 | let a = 0; 19 | let b = &a; 20 | let _ = v.iter().map(|_x| *b); 21 | let _ = v.iter().map(|_x| a.clone()); 22 | let _ = v.iter().map(|&_x| a); 23 | 24 | // Issue #498 25 | let _ = std::env::args().map(|v| v.clone()); 26 | } 27 | -------------------------------------------------------------------------------- /tests/ui/map_flatten.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all, clippy::pedantic)] 2 | #![allow(clippy::missing_docs_in_private_items)] 3 | 4 | fn main() { 5 | let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| 0..x).flatten().collect(); 6 | } 7 | -------------------------------------------------------------------------------- /tests/ui/map_flatten.stderr: -------------------------------------------------------------------------------- 1 | error: called `map(..).flatten()` on an `Iterator`. This is more succinctly expressed by calling `.flat_map(..)` 2 | --> $DIR/map_flatten.rs:5:21 3 | | 4 | LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| 0..x).flatten().collect(); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using flat_map instead: `vec![5_i8; 6].into_iter().flat_map(|x| 0..x)` 6 | | 7 | = note: `-D clippy::map-flatten` implied by `-D warnings` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/map_unit_fn.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused)] 2 | struct Mappable {} 3 | 4 | impl Mappable { 5 | pub fn map(&self) {} 6 | } 7 | 8 | fn main() { 9 | let m = Mappable {}; 10 | m.map(); 11 | } 12 | -------------------------------------------------------------------------------- /tests/ui/match_as_ref.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(unused)] 4 | #![warn(clippy::match_as_ref)] 5 | 6 | fn match_as_ref() { 7 | let owned: Option<()> = None; 8 | let borrowed: Option<&()> = owned.as_ref(); 9 | 10 | let mut mut_owned: Option<()> = None; 11 | let borrow_mut: Option<&mut ()> = mut_owned.as_mut(); 12 | } 13 | 14 | fn main() {} 15 | -------------------------------------------------------------------------------- /tests/ui/match_as_ref.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(unused)] 4 | #![warn(clippy::match_as_ref)] 5 | 6 | fn match_as_ref() { 7 | let owned: Option<()> = None; 8 | let borrowed: Option<&()> = match owned { 9 | None => None, 10 | Some(ref v) => Some(v), 11 | }; 12 | 13 | let mut mut_owned: Option<()> = None; 14 | let borrow_mut: Option<&mut ()> = match mut_owned { 15 | None => None, 16 | Some(ref mut v) => Some(v), 17 | }; 18 | } 19 | 20 | fn main() {} 21 | -------------------------------------------------------------------------------- /tests/ui/match_as_ref.stderr: -------------------------------------------------------------------------------- 1 | error: use as_ref() instead 2 | --> $DIR/match_as_ref.rs:8:33 3 | | 4 | LL | let borrowed: Option<&()> = match owned { 5 | | _________________________________^ 6 | LL | | None => None, 7 | LL | | Some(ref v) => Some(v), 8 | LL | | }; 9 | | |_____^ help: try this: `owned.as_ref()` 10 | | 11 | = note: `-D clippy::match-as-ref` implied by `-D warnings` 12 | 13 | error: use as_mut() instead 14 | --> $DIR/match_as_ref.rs:14:39 15 | | 16 | LL | let borrow_mut: Option<&mut ()> = match mut_owned { 17 | | _______________________________________^ 18 | LL | | None => None, 19 | LL | | Some(ref mut v) => Some(v), 20 | LL | | }; 21 | | |_____^ help: try this: `mut_owned.as_mut()` 22 | 23 | error: aborting due to 2 previous errors 24 | 25 | -------------------------------------------------------------------------------- /tests/ui/match_bool.rs: -------------------------------------------------------------------------------- 1 | fn match_bool() { 2 | let test: bool = true; 3 | 4 | match test { 5 | true => 0, 6 | false => 42, 7 | }; 8 | 9 | let option = 1; 10 | match option == 1 { 11 | true => 1, 12 | false => 0, 13 | }; 14 | 15 | match test { 16 | true => (), 17 | false => { 18 | println!("Noooo!"); 19 | }, 20 | }; 21 | 22 | match test { 23 | false => { 24 | println!("Noooo!"); 25 | }, 26 | _ => (), 27 | }; 28 | 29 | match test && test { 30 | false => { 31 | println!("Noooo!"); 32 | }, 33 | _ => (), 34 | }; 35 | 36 | match test { 37 | false => { 38 | println!("Noooo!"); 39 | }, 40 | true => { 41 | println!("Yes!"); 42 | }, 43 | }; 44 | 45 | // Not linted 46 | match option { 47 | 1...10 => 1, 48 | 11...20 => 2, 49 | _ => 3, 50 | }; 51 | } 52 | 53 | fn main() {} 54 | -------------------------------------------------------------------------------- /tests/ui/mem_forget.rs: -------------------------------------------------------------------------------- 1 | use std::rc::Rc; 2 | use std::sync::Arc; 3 | 4 | use std::mem as memstuff; 5 | use std::mem::forget as forgetSomething; 6 | 7 | #[warn(clippy::mem_forget)] 8 | #[allow(clippy::forget_copy)] 9 | fn main() { 10 | let five: i32 = 5; 11 | forgetSomething(five); 12 | 13 | let six: Arc = Arc::new(6); 14 | memstuff::forget(six); 15 | 16 | let seven: Rc = Rc::new(7); 17 | std::mem::forget(seven); 18 | 19 | let eight: Vec = vec![8]; 20 | forgetSomething(eight); 21 | 22 | std::mem::forget(7); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/mem_forget.stderr: -------------------------------------------------------------------------------- 1 | error: usage of mem::forget on Drop type 2 | --> $DIR/mem_forget.rs:14:5 3 | | 4 | LL | memstuff::forget(six); 5 | | ^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::mem-forget` implied by `-D warnings` 8 | 9 | error: usage of mem::forget on Drop type 10 | --> $DIR/mem_forget.rs:17:5 11 | | 12 | LL | std::mem::forget(seven); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^ 14 | 15 | error: usage of mem::forget on Drop type 16 | --> $DIR/mem_forget.rs:20:5 17 | | 18 | LL | forgetSomething(eight); 19 | | ^^^^^^^^^^^^^^^^^^^^^^ 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/mem_replace.fixed: -------------------------------------------------------------------------------- 1 | // Copyright 2014-2019 The Rust Project Developers. See the COPYRIGHT 2 | // file at the top-level directory of this distribution. 3 | // 4 | // Licensed under the Apache License, Version 2.0 or the MIT license 6 | // , at your 7 | // option. This file may not be copied, modified, or distributed 8 | // except according to those terms. 9 | 10 | // run-rustfix 11 | #![allow(unused_imports)] 12 | #![warn(clippy::all, clippy::style, clippy::mem_replace_option_with_none)] 13 | 14 | use std::mem; 15 | 16 | fn main() { 17 | let mut an_option = Some(1); 18 | let _ = an_option.take(); 19 | let an_option = &mut Some(1); 20 | let _ = an_option.take(); 21 | } 22 | -------------------------------------------------------------------------------- /tests/ui/mem_replace.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2014-2019 The Rust Project Developers. See the COPYRIGHT 2 | // file at the top-level directory of this distribution. 3 | // 4 | // Licensed under the Apache License, Version 2.0 or the MIT license 6 | // , at your 7 | // option. This file may not be copied, modified, or distributed 8 | // except according to those terms. 9 | 10 | // run-rustfix 11 | #![allow(unused_imports)] 12 | #![warn(clippy::all, clippy::style, clippy::mem_replace_option_with_none)] 13 | 14 | use std::mem; 15 | 16 | fn main() { 17 | let mut an_option = Some(1); 18 | let _ = mem::replace(&mut an_option, None); 19 | let an_option = &mut Some(1); 20 | let _ = mem::replace(an_option, None); 21 | } 22 | -------------------------------------------------------------------------------- /tests/ui/mem_replace.stderr: -------------------------------------------------------------------------------- 1 | error: replacing an `Option` with `None` 2 | --> $DIR/mem_replace.rs:18:13 3 | | 4 | LL | let _ = mem::replace(&mut an_option, None); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` 6 | | 7 | = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` 8 | 9 | error: replacing an `Option` with `None` 10 | --> $DIR/mem_replace.rs:20:13 11 | | 12 | LL | let _ = mem::replace(an_option, None); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/min_max.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all)] 2 | 3 | use std::cmp::max as my_max; 4 | use std::cmp::min as my_min; 5 | use std::cmp::{max, min}; 6 | 7 | const LARGE: usize = 3; 8 | 9 | fn main() { 10 | let x; 11 | x = 2usize; 12 | min(1, max(3, x)); 13 | min(max(3, x), 1); 14 | max(min(x, 1), 3); 15 | max(3, min(x, 1)); 16 | 17 | my_max(3, my_min(x, 1)); 18 | 19 | min(3, max(1, x)); // ok, could be 1, 2 or 3 depending on x 20 | 21 | min(1, max(LARGE, x)); // no error, we don't lookup consts here 22 | 23 | let y = 2isize; 24 | min(max(y, -1), 3); 25 | 26 | let s; 27 | s = "Hello"; 28 | 29 | min("Apple", max("Zoo", s)); 30 | max(min(s, "Apple"), "Zoo"); 31 | 32 | max("Apple", min(s, "Zoo")); // ok 33 | } 34 | -------------------------------------------------------------------------------- /tests/ui/missing-doc-crate-missing.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::missing_docs_in_private_items)] 2 | 3 | fn main() {} 4 | -------------------------------------------------------------------------------- /tests/ui/missing-doc-crate-missing.stderr: -------------------------------------------------------------------------------- 1 | error: missing documentation for crate 2 | --> $DIR/missing-doc-crate-missing.rs:1:1 3 | | 4 | LL | / #![warn(clippy::missing_docs_in_private_items)] 5 | LL | | 6 | LL | | fn main() {} 7 | | |____________^ 8 | | 9 | = note: `-D clippy::missing-docs-in-private-items` implied by `-D warnings` 10 | 11 | error: aborting due to previous error 12 | 13 | -------------------------------------------------------------------------------- /tests/ui/missing-doc-crate.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::missing_docs_in_private_items)] 2 | #![feature(external_doc)] 3 | #![doc(include = "../../README.md")] 4 | 5 | fn main() {} 6 | -------------------------------------------------------------------------------- /tests/ui/missing-doc-crate.stderr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/missing-doc-crate.stderr -------------------------------------------------------------------------------- /tests/ui/missing_const_for_fn/cant_be_const.stderr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/missing_const_for_fn/cant_be_const.stderr -------------------------------------------------------------------------------- /tests/ui/mistyped_literal_suffix.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(dead_code, unused_variables, clippy::excessive_precision)] 4 | 5 | fn main() { 6 | let fail14 = 2_i32; 7 | let fail15 = 4_i64; 8 | let fail16 = 7_i8; // 9 | let fail17 = 23_i16; // 10 | let ok18 = 23_128; 11 | 12 | let fail20 = 2_i8; // 13 | let fail21 = 4_i16; // 14 | 15 | let fail24 = 12.34_f64; 16 | let fail25 = 1E2_f32; 17 | let fail26 = 43E7_f64; 18 | let fail27 = 243E17_f32; 19 | #[allow(overflowing_literals)] 20 | let fail28 = 241_251_235E723_f64; 21 | let fail29 = 42_279.911_f32; 22 | } 23 | -------------------------------------------------------------------------------- /tests/ui/mistyped_literal_suffix.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(dead_code, unused_variables, clippy::excessive_precision)] 4 | 5 | fn main() { 6 | let fail14 = 2_32; 7 | let fail15 = 4_64; 8 | let fail16 = 7_8; // 9 | let fail17 = 23_16; // 10 | let ok18 = 23_128; 11 | 12 | let fail20 = 2__8; // 13 | let fail21 = 4___16; // 14 | 15 | let fail24 = 12.34_64; 16 | let fail25 = 1E2_32; 17 | let fail26 = 43E7_64; 18 | let fail27 = 243E17_32; 19 | #[allow(overflowing_literals)] 20 | let fail28 = 241251235E723_64; 21 | let fail29 = 42279.911_32; 22 | } 23 | -------------------------------------------------------------------------------- /tests/ui/module_inception.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::module_inception)] 2 | 3 | mod foo { 4 | mod bar { 5 | mod bar { 6 | mod foo {} 7 | } 8 | mod foo {} 9 | } 10 | mod foo { 11 | mod bar {} 12 | } 13 | } 14 | 15 | // No warning. See . 16 | mod bar { 17 | #[allow(clippy::module_inception)] 18 | mod bar {} 19 | } 20 | 21 | fn main() {} 22 | -------------------------------------------------------------------------------- /tests/ui/module_inception.stderr: -------------------------------------------------------------------------------- 1 | error: module has the same name as its containing module 2 | --> $DIR/module_inception.rs:5:9 3 | | 4 | LL | / mod bar { 5 | LL | | mod foo {} 6 | LL | | } 7 | | |_________^ 8 | | 9 | = note: `-D clippy::module-inception` implied by `-D warnings` 10 | 11 | error: module has the same name as its containing module 12 | --> $DIR/module_inception.rs:10:5 13 | | 14 | LL | / mod foo { 15 | LL | | mod bar {} 16 | LL | | } 17 | | |_____^ 18 | 19 | error: aborting due to 2 previous errors 20 | 21 | -------------------------------------------------------------------------------- /tests/ui/module_name_repetitions.rs: -------------------------------------------------------------------------------- 1 | // compile-flags: --test 2 | 3 | #![warn(clippy::module_name_repetitions)] 4 | #![allow(dead_code)] 5 | 6 | mod foo { 7 | pub fn foo() {} 8 | pub fn foo_bar() {} 9 | pub fn bar_foo() {} 10 | pub struct FooCake {} 11 | pub enum CakeFoo {} 12 | pub struct Foo7Bar; 13 | 14 | // Should not warn 15 | pub struct Foobar; 16 | } 17 | 18 | #[cfg(test)] 19 | mod test { 20 | #[test] 21 | fn it_works() { 22 | assert_eq!(2 + 2, 4); 23 | } 24 | } 25 | 26 | fn main() {} 27 | -------------------------------------------------------------------------------- /tests/ui/modulo_one.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::modulo_one)] 2 | #![allow(clippy::no_effect, clippy::unnecessary_operation)] 3 | 4 | fn main() { 5 | 10 % 1; 6 | 10 % 2; 7 | } 8 | -------------------------------------------------------------------------------- /tests/ui/modulo_one.stderr: -------------------------------------------------------------------------------- 1 | error: any number modulo 1 will be 0 2 | --> $DIR/modulo_one.rs:5:5 3 | | 4 | LL | 10 % 1; 5 | | ^^^^^^ 6 | | 7 | = note: `-D clippy::modulo-one` implied by `-D warnings` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/mut_from_ref.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused, clippy::trivially_copy_pass_by_ref)] 2 | #![warn(clippy::mut_from_ref)] 3 | 4 | struct Foo; 5 | 6 | impl Foo { 7 | fn this_wont_hurt_a_bit(&self) -> &mut Foo { 8 | unimplemented!() 9 | } 10 | } 11 | 12 | trait Ouch { 13 | fn ouch(x: &Foo) -> &mut Foo; 14 | } 15 | 16 | impl Ouch for Foo { 17 | fn ouch(x: &Foo) -> &mut Foo { 18 | unimplemented!() 19 | } 20 | } 21 | 22 | fn fail(x: &u32) -> &mut u16 { 23 | unimplemented!() 24 | } 25 | 26 | fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { 27 | unimplemented!() 28 | } 29 | 30 | fn fail_double<'a, 'b>(x: &'a u32, y: &'a u32, z: &'b mut u32) -> &'a mut u32 { 31 | unimplemented!() 32 | } 33 | 34 | // this is OK, because the result borrows y 35 | fn works<'a>(x: &u32, y: &'a mut u32) -> &'a mut u32 { 36 | unimplemented!() 37 | } 38 | 39 | // this is also OK, because the result could borrow y 40 | fn also_works<'a>(x: &'a u32, y: &'a mut u32) -> &'a mut u32 { 41 | unimplemented!() 42 | } 43 | 44 | fn main() { 45 | //TODO 46 | } 47 | -------------------------------------------------------------------------------- /tests/ui/mut_mut.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused, clippy::no_effect, clippy::unnecessary_operation)] 2 | #![warn(clippy::mut_mut)] 3 | 4 | fn fun(x: &mut &mut u32) -> bool { 5 | **x > 0 6 | } 7 | 8 | fn less_fun(x: *mut *mut u32) { 9 | let y = x; 10 | } 11 | 12 | macro_rules! mut_ptr { 13 | ($p:expr) => { 14 | &mut $p 15 | }; 16 | } 17 | 18 | #[allow(unused_mut, unused_variables)] 19 | fn main() { 20 | let mut x = &mut &mut 1u32; 21 | { 22 | let mut y = &mut x; 23 | } 24 | 25 | if fun(x) { 26 | let y: &mut &mut u32 = &mut &mut 2; 27 | **y + **x; 28 | } 29 | 30 | if fun(x) { 31 | let y: &mut &mut &mut u32 = &mut &mut &mut 2; 32 | ***y + **x; 33 | } 34 | 35 | let mut z = mut_ptr!(&mut 3u32); 36 | } 37 | 38 | fn issue939() { 39 | let array = [5, 6, 7, 8, 9]; 40 | let mut args = array.iter().skip(2); 41 | for &arg in &mut args { 42 | println!("{}", arg); 43 | } 44 | 45 | let args = &mut args; 46 | for arg in args { 47 | println!(":{}", arg); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/ui/mut_reference.stderr: -------------------------------------------------------------------------------- 1 | error: The function/method `takes_an_immutable_reference` doesn't need a mutable reference 2 | --> $DIR/mut_reference.rs:17:34 3 | | 4 | LL | takes_an_immutable_reference(&mut 42); 5 | | ^^^^^^^ 6 | | 7 | = note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings` 8 | 9 | error: The function/method `as_ptr` doesn't need a mutable reference 10 | --> $DIR/mut_reference.rs:19:12 11 | | 12 | LL | as_ptr(&mut 42); 13 | | ^^^^^^^ 14 | 15 | error: The function/method `takes_an_immutable_reference` doesn't need a mutable reference 16 | --> $DIR/mut_reference.rs:23:44 17 | | 18 | LL | my_struct.takes_an_immutable_reference(&mut 42); 19 | | ^^^^^^^ 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/mutex_atomic.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all)] 2 | #![warn(clippy::mutex_integer)] 3 | 4 | fn main() { 5 | use std::sync::Mutex; 6 | Mutex::new(true); 7 | Mutex::new(5usize); 8 | Mutex::new(9isize); 9 | let mut x = 4u32; 10 | Mutex::new(&x as *const u32); 11 | Mutex::new(&mut x as *mut u32); 12 | Mutex::new(0u32); 13 | Mutex::new(0i32); 14 | Mutex::new(0f32); // there are no float atomics, so this should not lint 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/my_lint.rs: -------------------------------------------------------------------------------- 1 | #[clippy::author] 2 | #[cfg(any(target_arch = "x86"))] 3 | pub struct Foo { 4 | x: u32, 5 | } 6 | 7 | fn main() {} 8 | -------------------------------------------------------------------------------- /tests/ui/needless_collect.rs: -------------------------------------------------------------------------------- 1 | use std::collections::{BTreeSet, HashMap, HashSet}; 2 | 3 | #[warn(clippy::needless_collect)] 4 | #[allow(unused_variables, clippy::iter_cloned_collect)] 5 | fn main() { 6 | let sample = [1; 5]; 7 | let len = sample.iter().collect::>().len(); 8 | if sample.iter().collect::>().is_empty() { 9 | // Empty 10 | } 11 | sample.iter().cloned().collect::>().contains(&1); 12 | sample.iter().map(|x| (x, x)).collect::>().len(); 13 | // Notice the `HashSet`--this should not be linted 14 | sample.iter().collect::>().len(); 15 | // Neither should this 16 | sample.iter().collect::>().len(); 17 | } 18 | -------------------------------------------------------------------------------- /tests/ui/needless_pass_by_value_proc_macro.rs: -------------------------------------------------------------------------------- 1 | #![crate_type = "proc-macro"] 2 | #![warn(clippy::needless_pass_by_value)] 3 | 4 | extern crate proc_macro; 5 | 6 | use proc_macro::TokenStream; 7 | 8 | #[proc_macro_derive(Foo)] 9 | pub fn foo(_input: TokenStream) -> TokenStream { 10 | unimplemented!() 11 | } 12 | 13 | #[proc_macro] 14 | pub fn bar(_input: TokenStream) -> TokenStream { 15 | unimplemented!() 16 | } 17 | 18 | #[proc_macro_attribute] 19 | pub fn baz(_args: TokenStream, _input: TokenStream) -> TokenStream { 20 | unimplemented!() 21 | } 22 | -------------------------------------------------------------------------------- /tests/ui/needless_return.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::needless_return)] 2 | 3 | fn test_end_of_fn() -> bool { 4 | if true { 5 | // no error! 6 | return true; 7 | } 8 | return true; 9 | } 10 | 11 | fn test_no_semicolon() -> bool { 12 | return true; 13 | } 14 | 15 | fn test_if_block() -> bool { 16 | if true { 17 | return true; 18 | } else { 19 | return false; 20 | } 21 | } 22 | 23 | fn test_match(x: bool) -> bool { 24 | match x { 25 | true => return false, 26 | false => { 27 | return true; 28 | }, 29 | } 30 | } 31 | 32 | fn test_closure() { 33 | let _ = || { 34 | return true; 35 | }; 36 | let _ = || return true; 37 | } 38 | 39 | fn main() { 40 | let _ = test_end_of_fn(); 41 | let _ = test_no_semicolon(); 42 | let _ = test_if_block(); 43 | let _ = test_match(true); 44 | test_closure(); 45 | } 46 | -------------------------------------------------------------------------------- /tests/ui/needless_update.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::needless_update)] 2 | #![allow(clippy::no_effect)] 3 | 4 | struct S { 5 | pub a: i32, 6 | pub b: i32, 7 | } 8 | 9 | fn main() { 10 | let base = S { a: 0, b: 0 }; 11 | S { ..base }; // no error 12 | S { a: 1, ..base }; // no error 13 | S { a: 1, b: 1, ..base }; 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/needless_update.stderr: -------------------------------------------------------------------------------- 1 | error: struct update has no effect, all the fields in the struct have already been specified 2 | --> $DIR/needless_update.rs:13:23 3 | | 4 | LL | S { a: 1, b: 1, ..base }; 5 | | ^^^^ 6 | | 7 | = note: `-D clippy::needless-update` implied by `-D warnings` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/neg_multiply.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::neg_multiply)] 2 | #![allow(clippy::no_effect, clippy::unnecessary_operation)] 3 | 4 | use std::ops::Mul; 5 | 6 | struct X; 7 | 8 | impl Mul for X { 9 | type Output = X; 10 | 11 | fn mul(self, _r: isize) -> Self { 12 | self 13 | } 14 | } 15 | 16 | impl Mul for isize { 17 | type Output = X; 18 | 19 | fn mul(self, _r: X) -> X { 20 | X 21 | } 22 | } 23 | 24 | fn main() { 25 | let x = 0; 26 | 27 | x * -1; 28 | 29 | -1 * x; 30 | 31 | -1 * -1; // should be ok 32 | 33 | X * -1; // should be ok 34 | -1 * X; // should also be ok 35 | } 36 | -------------------------------------------------------------------------------- /tests/ui/neg_multiply.stderr: -------------------------------------------------------------------------------- 1 | error: Negation by multiplying with -1 2 | --> $DIR/neg_multiply.rs:27:5 3 | | 4 | LL | x * -1; 5 | | ^^^^^^ 6 | | 7 | = note: `-D clippy::neg-multiply` implied by `-D warnings` 8 | 9 | error: Negation by multiplying with -1 10 | --> $DIR/neg_multiply.rs:29:5 11 | | 12 | LL | -1 * x; 13 | | ^^^^^^ 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/non_expressive_names.stdout: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/non_expressive_names.stdout -------------------------------------------------------------------------------- /tests/ui/ok_expect.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | 3 | struct MyError(()); // doesn't implement Debug 4 | 5 | #[derive(Debug)] 6 | struct MyErrorWithParam { 7 | x: T, 8 | } 9 | 10 | fn main() { 11 | let res: Result = Ok(0); 12 | let _ = res.unwrap(); 13 | 14 | res.ok().expect("disaster!"); 15 | // the following should not warn, since `expect` isn't implemented unless 16 | // the error type implements `Debug` 17 | let res2: Result = Ok(0); 18 | res2.ok().expect("oh noes!"); 19 | let res3: Result> = Ok(0); 20 | res3.ok().expect("whoof"); 21 | let res4: Result = Ok(0); 22 | res4.ok().expect("argh"); 23 | let res5: io::Result = Ok(0); 24 | res5.ok().expect("oops"); 25 | let res6: Result = Ok(0); 26 | res6.ok().expect("meh"); 27 | } 28 | -------------------------------------------------------------------------------- /tests/ui/ok_if_let.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::if_let_some_result)] 2 | 3 | fn str_to_int(x: &str) -> i32 { 4 | if let Some(y) = x.parse().ok() { 5 | y 6 | } else { 7 | 0 8 | } 9 | } 10 | 11 | fn str_to_int_ok(x: &str) -> i32 { 12 | if let Ok(y) = x.parse() { 13 | y 14 | } else { 15 | 0 16 | } 17 | } 18 | 19 | fn main() { 20 | let y = str_to_int("1"); 21 | let z = str_to_int_ok("2"); 22 | println!("{}{}", y, z); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/ok_if_let.stderr: -------------------------------------------------------------------------------- 1 | error: Matching on `Some` with `ok()` is redundant 2 | --> $DIR/ok_if_let.rs:4:5 3 | | 4 | LL | / if let Some(y) = x.parse().ok() { 5 | LL | | y 6 | LL | | } else { 7 | LL | | 0 8 | LL | | } 9 | | |_____^ 10 | | 11 | = note: `-D clippy::if-let-some-result` implied by `-D warnings` 12 | = help: Consider matching on `Ok(y)` and removing the call to `ok` instead 13 | 14 | error: aborting due to previous error 15 | 16 | -------------------------------------------------------------------------------- /tests/ui/op_ref.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused_variables, clippy::blacklisted_name)] 2 | 3 | use std::collections::HashSet; 4 | 5 | fn main() { 6 | let tracked_fds: HashSet = HashSet::new(); 7 | let new_fds = HashSet::new(); 8 | let unwanted = &tracked_fds - &new_fds; 9 | 10 | let foo = &5 - &6; 11 | 12 | let bar = String::new(); 13 | let bar = "foo" == &bar; 14 | 15 | let a = "a".to_string(); 16 | let b = "a"; 17 | 18 | if b < &a { 19 | println!("OK"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/ui/op_ref.stderr: -------------------------------------------------------------------------------- 1 | error: needlessly taken reference of both operands 2 | --> $DIR/op_ref.rs:10:15 3 | | 4 | LL | let foo = &5 - &6; 5 | | ^^^^^^^ 6 | | 7 | = note: `-D clippy::op-ref` implied by `-D warnings` 8 | help: use the values directly 9 | | 10 | LL | let foo = 5 - 6; 11 | | ^ ^ 12 | 13 | error: taken reference of right operand 14 | --> $DIR/op_ref.rs:18:8 15 | | 16 | LL | if b < &a { 17 | | ^^^^-- 18 | | | 19 | | help: use the right value directly: `a` 20 | 21 | error: aborting due to 2 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/open_options.rs: -------------------------------------------------------------------------------- 1 | use std::fs::OpenOptions; 2 | 3 | #[allow(unused_must_use)] 4 | #[warn(clippy::nonsensical_open_options)] 5 | fn main() { 6 | OpenOptions::new().read(true).truncate(true).open("foo.txt"); 7 | OpenOptions::new().append(true).truncate(true).open("foo.txt"); 8 | 9 | OpenOptions::new().read(true).read(false).open("foo.txt"); 10 | OpenOptions::new().create(true).create(false).open("foo.txt"); 11 | OpenOptions::new().write(true).write(false).open("foo.txt"); 12 | OpenOptions::new().append(true).append(false).open("foo.txt"); 13 | OpenOptions::new().truncate(true).truncate(false).open("foo.txt"); 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/option_map_or_none.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn main() { 4 | let opt = Some(1); 5 | 6 | // Check `OPTION_MAP_OR_NONE`. 7 | // Single line case. 8 | let _ = opt.and_then(|x| Some(x + 1)); 9 | // Multi-line case. 10 | #[rustfmt::skip] 11 | let _ = opt.and_then(|x| { 12 | Some(x + 1) 13 | }); 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/option_map_or_none.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn main() { 4 | let opt = Some(1); 5 | 6 | // Check `OPTION_MAP_OR_NONE`. 7 | // Single line case. 8 | let _ = opt.map_or(None, |x| Some(x + 1)); 9 | // Multi-line case. 10 | #[rustfmt::skip] 11 | let _ = opt.map_or(None, |x| { 12 | Some(x + 1) 13 | }); 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/option_map_or_none.stderr: -------------------------------------------------------------------------------- 1 | error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead 2 | --> $DIR/option_map_or_none.rs:8:13 3 | | 4 | LL | let _ = opt.map_or(None, |x| Some(x + 1)); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using and_then instead: `opt.and_then(|x| Some(x + 1))` 6 | | 7 | = note: `-D clippy::option-map-or-none` implied by `-D warnings` 8 | 9 | error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead 10 | --> $DIR/option_map_or_none.rs:11:13 11 | | 12 | LL | let _ = opt.map_or(None, |x| { 13 | | _____________^ 14 | LL | | Some(x + 1) 15 | LL | | }); 16 | | |_________________________^ 17 | help: try using and_then instead 18 | | 19 | LL | let _ = opt.and_then(|x| { 20 | LL | Some(x + 1) 21 | LL | }); 22 | | 23 | 24 | error: aborting due to 2 previous errors 25 | 26 | -------------------------------------------------------------------------------- /tests/ui/overflow_check_conditional.rs: -------------------------------------------------------------------------------- 1 | #![allow(clippy::many_single_char_names)] 2 | #![warn(clippy::overflow_check_conditional)] 3 | 4 | fn main() { 5 | let a: u32 = 1; 6 | let b: u32 = 2; 7 | let c: u32 = 3; 8 | if a + b < a {} 9 | if a > a + b {} 10 | if a + b < b {} 11 | if b > a + b {} 12 | if a - b > b {} 13 | if b < a - b {} 14 | if a - b > a {} 15 | if a < a - b {} 16 | if a + b < c {} 17 | if c > a + b {} 18 | if a - b < c {} 19 | if c > a - b {} 20 | let i = 1.1; 21 | let j = 2.2; 22 | if i + j < i {} 23 | if i - j < i {} 24 | if i > i + j {} 25 | if i - j < i {} 26 | } 27 | -------------------------------------------------------------------------------- /tests/ui/partialeq_ne_impl.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | 3 | struct Foo; 4 | 5 | impl PartialEq for Foo { 6 | fn eq(&self, _: &Foo) -> bool { 7 | true 8 | } 9 | fn ne(&self, _: &Foo) -> bool { 10 | false 11 | } 12 | } 13 | 14 | struct Bar; 15 | 16 | impl PartialEq for Bar { 17 | fn eq(&self, _: &Bar) -> bool { 18 | true 19 | } 20 | #[allow(clippy::partialeq_ne_impl)] 21 | fn ne(&self, _: &Bar) -> bool { 22 | false 23 | } 24 | } 25 | 26 | fn main() {} 27 | -------------------------------------------------------------------------------- /tests/ui/partialeq_ne_impl.stderr: -------------------------------------------------------------------------------- 1 | error: re-implementing `PartialEq::ne` is unnecessary 2 | --> $DIR/partialeq_ne_impl.rs:9:5 3 | | 4 | LL | / fn ne(&self, _: &Foo) -> bool { 5 | LL | | false 6 | LL | | } 7 | | |_____^ 8 | | 9 | = note: `-D clippy::partialeq-ne-impl` implied by `-D warnings` 10 | 11 | error: aborting due to previous error 12 | 13 | -------------------------------------------------------------------------------- /tests/ui/path_buf_push_overwrite.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | use std::path::PathBuf; 3 | 4 | #[warn(clippy::all, clippy::path_buf_push_overwrite)] 5 | fn main() { 6 | let mut x = PathBuf::from("/foo"); 7 | x.push("bar"); 8 | } 9 | -------------------------------------------------------------------------------- /tests/ui/path_buf_push_overwrite.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | use std::path::PathBuf; 3 | 4 | #[warn(clippy::all, clippy::path_buf_push_overwrite)] 5 | fn main() { 6 | let mut x = PathBuf::from("/foo"); 7 | x.push("/bar"); 8 | } 9 | -------------------------------------------------------------------------------- /tests/ui/path_buf_push_overwrite.stderr: -------------------------------------------------------------------------------- 1 | error: Calling `push` with '/' or '/' (file system root) will overwrite the previous path definition 2 | --> $DIR/path_buf_push_overwrite.rs:7:12 3 | | 4 | LL | x.push("/bar"); 5 | | ^^^^^^ help: try: `"bar"` 6 | | 7 | = note: `-D clippy::path-buf-push-overwrite` implied by `-D warnings` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/patterns.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused)] 2 | #![warn(clippy::all)] 3 | 4 | fn main() { 5 | let v = Some(true); 6 | match v { 7 | Some(x) => (), 8 | y @ _ => (), 9 | } 10 | match v { 11 | Some(x) => (), 12 | y @ None => (), // no error 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/patterns.stderr: -------------------------------------------------------------------------------- 1 | error: the `y @ _` pattern can be written as just `y` 2 | --> $DIR/patterns.rs:8:9 3 | | 4 | LL | y @ _ => (), 5 | | ^^^^^ 6 | | 7 | = note: `-D clippy::redundant-pattern` implied by `-D warnings` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/precedence.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #![warn(clippy::precedence)] 3 | #![allow(unused_must_use, clippy::no_effect, clippy::unnecessary_operation)] 4 | #![allow(clippy::identity_op)] 5 | #![allow(clippy::eq_op)] 6 | 7 | macro_rules! trip { 8 | ($a:expr) => { 9 | match $a & 0b1111_1111u8 { 10 | 0 => println!("a is zero ({})", $a), 11 | _ => println!("a is {}", $a), 12 | } 13 | }; 14 | } 15 | 16 | fn main() { 17 | 1 << (2 + 3); 18 | (1 + 2) << 3; 19 | 4 >> (1 + 1); 20 | (1 + 3) >> 2; 21 | 1 ^ (1 - 1); 22 | 3 | (2 - 1); 23 | 3 & (5 - 2); 24 | -(1i32.abs()); 25 | -(1f32.abs()); 26 | 27 | // These should not trigger an error 28 | let _ = (-1i32).abs(); 29 | let _ = (-1f32).abs(); 30 | let _ = -(1i32).abs(); 31 | let _ = -(1f32).abs(); 32 | let _ = -(1i32.abs()); 33 | let _ = -(1f32.abs()); 34 | 35 | let b = 3; 36 | trip!(b * 8); 37 | } 38 | -------------------------------------------------------------------------------- /tests/ui/precedence.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | #![warn(clippy::precedence)] 3 | #![allow(unused_must_use, clippy::no_effect, clippy::unnecessary_operation)] 4 | #![allow(clippy::identity_op)] 5 | #![allow(clippy::eq_op)] 6 | 7 | macro_rules! trip { 8 | ($a:expr) => { 9 | match $a & 0b1111_1111u8 { 10 | 0 => println!("a is zero ({})", $a), 11 | _ => println!("a is {}", $a), 12 | } 13 | }; 14 | } 15 | 16 | fn main() { 17 | 1 << 2 + 3; 18 | 1 + 2 << 3; 19 | 4 >> 1 + 1; 20 | 1 + 3 >> 2; 21 | 1 ^ 1 - 1; 22 | 3 | 2 - 1; 23 | 3 & 5 - 2; 24 | -1i32.abs(); 25 | -1f32.abs(); 26 | 27 | // These should not trigger an error 28 | let _ = (-1i32).abs(); 29 | let _ = (-1f32).abs(); 30 | let _ = -(1i32).abs(); 31 | let _ = -(1f32).abs(); 32 | let _ = -(1i32.abs()); 33 | let _ = -(1f32.abs()); 34 | 35 | let b = 3; 36 | trip!(b * 8); 37 | } 38 | -------------------------------------------------------------------------------- /tests/ui/print.rs: -------------------------------------------------------------------------------- 1 | #![allow(clippy::print_literal, clippy::write_literal)] 2 | #![warn(clippy::print_stdout, clippy::use_debug)] 3 | 4 | use std::fmt::{Debug, Display, Formatter, Result}; 5 | 6 | #[allow(dead_code)] 7 | struct Foo; 8 | 9 | impl Display for Foo { 10 | fn fmt(&self, f: &mut Formatter) -> Result { 11 | write!(f, "{:?}", 43.1415) 12 | } 13 | } 14 | 15 | impl Debug for Foo { 16 | fn fmt(&self, f: &mut Formatter) -> Result { 17 | // ok, we can use `Debug` formatting in `Debug` implementations 18 | write!(f, "{:?}", 42.718) 19 | } 20 | } 21 | 22 | fn main() { 23 | println!("Hello"); 24 | print!("Hello"); 25 | 26 | print!("Hello {}", "World"); 27 | 28 | print!("Hello {:?}", "World"); 29 | 30 | print!("Hello {:#?}", "#orld"); 31 | 32 | assert_eq!(42, 1337); 33 | 34 | vec![1, 2]; 35 | } 36 | -------------------------------------------------------------------------------- /tests/ui/println_empty_string.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn main() { 4 | println!(); 5 | println!(); 6 | 7 | match "a" { 8 | _ => println!(), 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/ui/println_empty_string.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn main() { 4 | println!(); 5 | println!(""); 6 | 7 | match "a" { 8 | _ => println!(""), 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/ui/println_empty_string.stderr: -------------------------------------------------------------------------------- 1 | error: using `println!("")` 2 | --> $DIR/println_empty_string.rs:5:5 3 | | 4 | LL | println!(""); 5 | | ^^^^^^^^^^^^ help: replace it with: `println!()` 6 | | 7 | = note: `-D clippy::println-empty-string` implied by `-D warnings` 8 | 9 | error: using `println!("")` 10 | --> $DIR/println_empty_string.rs:8:14 11 | | 12 | LL | _ => println!(""), 13 | | ^^^^^^^^^^^^ help: replace it with: `println!()` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/proc_macro.rs: -------------------------------------------------------------------------------- 1 | //! Check that we correctly lint procedural macros. 2 | 3 | #![crate_type = "proc-macro"] 4 | 5 | #[allow(dead_code)] 6 | fn f() { 7 | let _x = 3.14; 8 | } 9 | -------------------------------------------------------------------------------- /tests/ui/proc_macro.stderr: -------------------------------------------------------------------------------- 1 | error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly 2 | --> $DIR/proc_macro.rs:7:14 3 | | 4 | LL | let _x = 3.14; 5 | | ^^^^ 6 | | 7 | = note: #[deny(clippy::approx_constant)] on by default 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/ptr_offset_with_cast.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn main() { 4 | let vec = vec![b'a', b'b', b'c']; 5 | let ptr = vec.as_ptr(); 6 | 7 | let offset_u8 = 1_u8; 8 | let offset_usize = 1_usize; 9 | let offset_isize = 1_isize; 10 | 11 | unsafe { 12 | ptr.add(offset_usize); 13 | ptr.offset(offset_isize as isize); 14 | ptr.offset(offset_u8 as isize); 15 | 16 | ptr.wrapping_add(offset_usize); 17 | ptr.wrapping_offset(offset_isize as isize); 18 | ptr.wrapping_offset(offset_u8 as isize); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/ui/ptr_offset_with_cast.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | fn main() { 4 | let vec = vec![b'a', b'b', b'c']; 5 | let ptr = vec.as_ptr(); 6 | 7 | let offset_u8 = 1_u8; 8 | let offset_usize = 1_usize; 9 | let offset_isize = 1_isize; 10 | 11 | unsafe { 12 | ptr.offset(offset_usize as isize); 13 | ptr.offset(offset_isize as isize); 14 | ptr.offset(offset_u8 as isize); 15 | 16 | ptr.wrapping_offset(offset_usize as isize); 17 | ptr.wrapping_offset(offset_isize as isize); 18 | ptr.wrapping_offset(offset_u8 as isize); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/ui/ptr_offset_with_cast.stderr: -------------------------------------------------------------------------------- 1 | error: use of `offset` with a `usize` casted to an `isize` 2 | --> $DIR/ptr_offset_with_cast.rs:12:9 3 | | 4 | LL | ptr.offset(offset_usize as isize); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr.add(offset_usize)` 6 | | 7 | = note: `-D clippy::ptr-offset-with-cast` implied by `-D warnings` 8 | 9 | error: use of `wrapping_offset` with a `usize` casted to an `isize` 10 | --> $DIR/ptr_offset_with_cast.rs:16:9 11 | | 12 | LL | ptr.wrapping_offset(offset_usize as isize); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr.wrapping_add(offset_usize)` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/range.rs: -------------------------------------------------------------------------------- 1 | struct NotARange; 2 | impl NotARange { 3 | fn step_by(&self, _: u32) {} 4 | } 5 | 6 | #[warn(clippy::iterator_step_by_zero, clippy::range_zip_with_len)] 7 | fn main() { 8 | let _ = (0..1).step_by(0); 9 | // No warning for non-zero step 10 | let _ = (0..1).step_by(1); 11 | 12 | let _ = (1..).step_by(0); 13 | let _ = (1..=2).step_by(0); 14 | 15 | let x = 0..1; 16 | let _ = x.step_by(0); 17 | 18 | // No error, not a range. 19 | let y = NotARange; 20 | y.step_by(0); 21 | 22 | let v1 = vec![1, 2, 3]; 23 | let v2 = vec![4, 5]; 24 | let _x = v1.iter().zip(0..v1.len()); 25 | let _y = v1.iter().zip(0..v2.len()); // No error 26 | 27 | // check const eval 28 | let _ = v1.iter().step_by(2 / 3); 29 | } 30 | 31 | #[allow(unused)] 32 | fn no_panic_with_fake_range_types() { 33 | struct Range { 34 | foo: i32, 35 | } 36 | 37 | let _ = Range { foo: 0 }; 38 | } 39 | -------------------------------------------------------------------------------- /tests/ui/range_plus_minus_one.rs: -------------------------------------------------------------------------------- 1 | fn f() -> usize { 2 | 42 3 | } 4 | 5 | #[warn(clippy::range_plus_one)] 6 | fn main() { 7 | for _ in 0..2 {} 8 | for _ in 0..=2 {} 9 | 10 | for _ in 0..3 + 1 {} 11 | for _ in 0..=3 + 1 {} 12 | 13 | for _ in 0..1 + 5 {} 14 | for _ in 0..=1 + 5 {} 15 | 16 | for _ in 1..1 + 1 {} 17 | for _ in 1..=1 + 1 {} 18 | 19 | for _ in 0..13 + 13 {} 20 | for _ in 0..=13 - 7 {} 21 | 22 | for _ in 0..(1 + f()) {} 23 | for _ in 0..=(1 + f()) {} 24 | 25 | let _ = ..11 - 1; 26 | let _ = ..=11 - 1; 27 | let _ = ..=(11 - 1); 28 | let _ = (1..11 + 1); 29 | let _ = (f() + 1)..(f() + 1); 30 | 31 | let mut vec: Vec<()> = std::vec::Vec::new(); 32 | vec.drain(..); 33 | } 34 | -------------------------------------------------------------------------------- /tests/ui/redundant_closure_call.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::redundant_closure_call)] 2 | 3 | fn main() { 4 | let a = (|| 42)(); 5 | 6 | let mut i = 1; 7 | let mut k = (|m| m + 1)(i); 8 | 9 | k = (|a, b| a * b)(1, 5); 10 | 11 | let closure = || 32; 12 | i = closure(); 13 | 14 | let closure = |i| i + 1; 15 | i = closure(3); 16 | 17 | i = closure(4); 18 | 19 | #[allow(clippy::needless_return)] 20 | (|| return 2)(); 21 | (|| -> Option { None? })(); 22 | (|| -> Result { r#try!(Err(2)) })(); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/rename.rs: -------------------------------------------------------------------------------- 1 | #![allow(stutter)] 2 | #![warn(clippy::cyclomatic_complexity)] 3 | 4 | #[warn(clippy::stutter)] 5 | fn main() {} 6 | 7 | #[warn(clippy::new_without_default_derive)] 8 | struct Foo; 9 | 10 | impl Foo { 11 | fn new() -> Self { 12 | Foo 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/renamed_builtin_attr.rs: -------------------------------------------------------------------------------- 1 | #[clippy::cyclomatic_complexity = "1"] 2 | fn main() {} 3 | -------------------------------------------------------------------------------- /tests/ui/renamed_builtin_attr.stderr: -------------------------------------------------------------------------------- 1 | error: Usage of deprecated attribute 2 | --> $DIR/renamed_builtin_attr.rs:1:11 3 | | 4 | LL | #[clippy::cyclomatic_complexity = "1"] 5 | | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `cognitive_complexity` 6 | 7 | error: aborting due to previous error 8 | 9 | -------------------------------------------------------------------------------- /tests/ui/result_map_unwrap_or_else.rs: -------------------------------------------------------------------------------- 1 | // aux-build:option_helpers.rs 2 | 3 | //! Checks implementation of `RESULT_MAP_UNWRAP_OR_ELSE` 4 | 5 | #![warn(clippy::result_map_unwrap_or_else)] 6 | 7 | #[macro_use] 8 | extern crate option_helpers; 9 | 10 | fn result_methods() { 11 | let res: Result = Ok(1); 12 | 13 | // Check RESULT_MAP_UNWRAP_OR_ELSE 14 | // single line case 15 | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line 16 | // multi line cases 17 | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); 18 | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); 19 | // macro case 20 | let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint 21 | } 22 | 23 | fn main() {} 24 | -------------------------------------------------------------------------------- /tests/ui/serde.stderr: -------------------------------------------------------------------------------- 1 | error: you should not implement `visit_string` without also implementing `visit_str` 2 | --> $DIR/serde.rs:39:5 3 | | 4 | LL | / fn visit_string(self, _v: String) -> Result 5 | LL | | where 6 | LL | | E: serde::de::Error, 7 | LL | | { 8 | LL | | unimplemented!() 9 | LL | | } 10 | | |_____^ 11 | | 12 | = note: `-D clippy::serde-api-misuse` implied by `-D warnings` 13 | 14 | error: aborting due to previous error 15 | 16 | -------------------------------------------------------------------------------- /tests/ui/shadow.rs: -------------------------------------------------------------------------------- 1 | #![warn( 2 | clippy::all, 3 | clippy::pedantic, 4 | clippy::shadow_same, 5 | clippy::shadow_reuse, 6 | clippy::shadow_unrelated 7 | )] 8 | #![allow(unused_parens, unused_variables, clippy::missing_docs_in_private_items)] 9 | 10 | fn id(x: T) -> T { 11 | x 12 | } 13 | 14 | fn first(x: (isize, isize)) -> isize { 15 | x.0 16 | } 17 | 18 | fn main() { 19 | let mut x = 1; 20 | let x = &mut x; 21 | let x = { x }; 22 | let x = (&*x); 23 | let x = { *x + 1 }; 24 | let x = id(x); 25 | let x = (1, x); 26 | let x = first(x); 27 | let y = 1; 28 | let x = y; 29 | 30 | let x; 31 | x = 42; 32 | 33 | let o = Some(1_u8); 34 | 35 | if let Some(p) = o { 36 | assert_eq!(1, p); 37 | } 38 | match o { 39 | Some(p) => p, // no error, because the p above is in its own scope 40 | None => 0, 41 | }; 42 | 43 | match (x, o) { 44 | (1, Some(a)) | (a, Some(1)) => (), // no error though `a` appears twice 45 | _ => (), 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/ui/short_circuit_statement.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::short_circuit_statement)] 2 | 3 | fn main() { 4 | f() && g(); 5 | f() || g(); 6 | 1 == 2 || g(); 7 | } 8 | 9 | fn f() -> bool { 10 | true 11 | } 12 | 13 | fn g() -> bool { 14 | false 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/short_circuit_statement.stderr: -------------------------------------------------------------------------------- 1 | error: boolean short circuit operator in statement may be clearer using an explicit test 2 | --> $DIR/short_circuit_statement.rs:4:5 3 | | 4 | LL | f() && g(); 5 | | ^^^^^^^^^^^ help: replace it with: `if f() { g(); }` 6 | | 7 | = note: `-D clippy::short-circuit-statement` implied by `-D warnings` 8 | 9 | error: boolean short circuit operator in statement may be clearer using an explicit test 10 | --> $DIR/short_circuit_statement.rs:5:5 11 | | 12 | LL | f() || g(); 13 | | ^^^^^^^^^^^ help: replace it with: `if !f() { g(); }` 14 | 15 | error: boolean short circuit operator in statement may be clearer using an explicit test 16 | --> $DIR/short_circuit_statement.rs:6:5 17 | | 18 | LL | 1 == 2 || g(); 19 | | ^^^^^^^^^^^^^^ help: replace it with: `if !(1 == 2) { g(); }` 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/single_match_else.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::single_match_else)] 2 | 3 | enum ExprNode { 4 | ExprAddrOf, 5 | Butterflies, 6 | Unicorns, 7 | } 8 | 9 | static NODE: ExprNode = ExprNode::Unicorns; 10 | 11 | fn unwrap_addr() -> Option<&'static ExprNode> { 12 | match ExprNode::Butterflies { 13 | ExprNode::ExprAddrOf => Some(&NODE), 14 | _ => { 15 | let x = 5; 16 | None 17 | }, 18 | } 19 | } 20 | 21 | fn main() {} 22 | -------------------------------------------------------------------------------- /tests/ui/single_match_else.stderr: -------------------------------------------------------------------------------- 1 | error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let` 2 | --> $DIR/single_match_else.rs:12:5 3 | | 4 | LL | / match ExprNode::Butterflies { 5 | LL | | ExprNode::ExprAddrOf => Some(&NODE), 6 | LL | | _ => { 7 | LL | | let x = 5; 8 | LL | | None 9 | LL | | }, 10 | LL | | } 11 | | |_____^ 12 | | 13 | = note: `-D clippy::single-match-else` implied by `-D warnings` 14 | help: try this 15 | | 16 | LL | if let ExprNode::ExprAddrOf = ExprNode::Butterflies { Some(&NODE) } else { 17 | LL | let x = 5; 18 | LL | None 19 | LL | } 20 | | 21 | 22 | error: aborting due to previous error 23 | 24 | -------------------------------------------------------------------------------- /tests/ui/slow_vector_initialization.stdout: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/slow_vector_initialization.stdout -------------------------------------------------------------------------------- /tests/ui/string_extend.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #[derive(Copy, Clone)] 4 | struct HasChars; 5 | 6 | impl HasChars { 7 | fn chars(self) -> std::str::Chars<'static> { 8 | "HasChars".chars() 9 | } 10 | } 11 | 12 | fn main() { 13 | let abc = "abc"; 14 | let def = String::from("def"); 15 | let mut s = String::new(); 16 | 17 | s.push_str(abc); 18 | s.push_str(abc); 19 | 20 | s.push_str("abc"); 21 | s.push_str("abc"); 22 | 23 | s.push_str(&def); 24 | s.push_str(&def); 25 | 26 | s.extend(abc.chars().skip(1)); 27 | s.extend("abc".chars().skip(1)); 28 | s.extend(['a', 'b', 'c'].iter()); 29 | 30 | let f = HasChars; 31 | s.extend(f.chars()); 32 | } 33 | -------------------------------------------------------------------------------- /tests/ui/string_extend.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #[derive(Copy, Clone)] 4 | struct HasChars; 5 | 6 | impl HasChars { 7 | fn chars(self) -> std::str::Chars<'static> { 8 | "HasChars".chars() 9 | } 10 | } 11 | 12 | fn main() { 13 | let abc = "abc"; 14 | let def = String::from("def"); 15 | let mut s = String::new(); 16 | 17 | s.push_str(abc); 18 | s.extend(abc.chars()); 19 | 20 | s.push_str("abc"); 21 | s.extend("abc".chars()); 22 | 23 | s.push_str(&def); 24 | s.extend(def.chars()); 25 | 26 | s.extend(abc.chars().skip(1)); 27 | s.extend("abc".chars().skip(1)); 28 | s.extend(['a', 'b', 'c'].iter()); 29 | 30 | let f = HasChars; 31 | s.extend(f.chars()); 32 | } 33 | -------------------------------------------------------------------------------- /tests/ui/string_extend.stderr: -------------------------------------------------------------------------------- 1 | error: calling `.extend(_.chars())` 2 | --> $DIR/string_extend.rs:18:5 3 | | 4 | LL | s.extend(abc.chars()); 5 | | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(abc)` 6 | | 7 | = note: `-D clippy::string-extend-chars` implied by `-D warnings` 8 | 9 | error: calling `.extend(_.chars())` 10 | --> $DIR/string_extend.rs:21:5 11 | | 12 | LL | s.extend("abc".chars()); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str("abc")` 14 | 15 | error: calling `.extend(_.chars())` 16 | --> $DIR/string_extend.rs:24:5 17 | | 18 | LL | s.extend(def.chars()); 19 | | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&def)` 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/string_lit_as_bytes.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(dead_code, unused_variables)] 4 | #![warn(clippy::string_lit_as_bytes)] 5 | 6 | fn str_lit_as_bytes() { 7 | let bs = b"hello there"; 8 | 9 | let bs = br###"raw string with three ### in it and some " ""###; 10 | 11 | // no warning, because this cannot be written as a byte string literal: 12 | let ubs = "☃".as_bytes(); 13 | 14 | let strify = stringify!(foobar).as_bytes(); 15 | 16 | let includestr = include_bytes!("entry.rs"); 17 | } 18 | 19 | fn main() {} 20 | -------------------------------------------------------------------------------- /tests/ui/string_lit_as_bytes.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(dead_code, unused_variables)] 4 | #![warn(clippy::string_lit_as_bytes)] 5 | 6 | fn str_lit_as_bytes() { 7 | let bs = "hello there".as_bytes(); 8 | 9 | let bs = r###"raw string with three ### in it and some " ""###.as_bytes(); 10 | 11 | // no warning, because this cannot be written as a byte string literal: 12 | let ubs = "☃".as_bytes(); 13 | 14 | let strify = stringify!(foobar).as_bytes(); 15 | 16 | let includestr = include_str!("entry.rs").as_bytes(); 17 | } 18 | 19 | fn main() {} 20 | -------------------------------------------------------------------------------- /tests/ui/suspicious_arithmetic_impl.stderr: -------------------------------------------------------------------------------- 1 | error: Suspicious use of binary operator in `Add` impl 2 | --> $DIR/suspicious_arithmetic_impl.rs:11:20 3 | | 4 | LL | Foo(self.0 - other.0) 5 | | ^ 6 | | 7 | = note: `-D clippy::suspicious-arithmetic-impl` implied by `-D warnings` 8 | 9 | error: Suspicious use of binary operator in `AddAssign` impl 10 | --> $DIR/suspicious_arithmetic_impl.rs:17:23 11 | | 12 | LL | *self = *self - other; 13 | | ^ 14 | | 15 | = note: #[deny(clippy::suspicious_op_assign_impl)] on by default 16 | 17 | error: aborting due to 2 previous errors 18 | 19 | -------------------------------------------------------------------------------- /tests/ui/swap.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all)] 2 | #![allow(clippy::blacklisted_name, unused_assignments)] 3 | 4 | struct Foo(u32); 5 | 6 | fn array() { 7 | let mut foo = [1, 2]; 8 | let temp = foo[0]; 9 | foo[0] = foo[1]; 10 | foo[1] = temp; 11 | 12 | foo.swap(0, 1); 13 | } 14 | 15 | fn slice() { 16 | let foo = &mut [1, 2]; 17 | let temp = foo[0]; 18 | foo[0] = foo[1]; 19 | foo[1] = temp; 20 | 21 | foo.swap(0, 1); 22 | } 23 | 24 | fn vec() { 25 | let mut foo = vec![1, 2]; 26 | let temp = foo[0]; 27 | foo[0] = foo[1]; 28 | foo[1] = temp; 29 | 30 | foo.swap(0, 1); 31 | } 32 | 33 | #[rustfmt::skip] 34 | fn main() { 35 | array(); 36 | slice(); 37 | vec(); 38 | 39 | let mut a = 42; 40 | let mut b = 1337; 41 | 42 | a = b; 43 | b = a; 44 | 45 | ; let t = a; 46 | a = b; 47 | b = t; 48 | 49 | let mut c = Foo(42); 50 | 51 | c.0 = a; 52 | a = c.0; 53 | 54 | ; let t = c.0; 55 | c.0 = a; 56 | a = t; 57 | } 58 | -------------------------------------------------------------------------------- /tests/ui/toplevel_ref_arg.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all)] 2 | #![allow(unused)] 3 | 4 | fn the_answer(ref mut x: u8) { 5 | *x = 42; 6 | } 7 | 8 | fn main() { 9 | let mut x = 0; 10 | the_answer(x); 11 | // Closures should not warn 12 | let y = |ref x| println!("{:?}", x); 13 | y(1u8); 14 | 15 | let ref x = 1; 16 | 17 | let ref y: (&_, u8) = (&1, 2); 18 | 19 | let ref z = 1 + 2; 20 | 21 | let ref mut z = 1 + 2; 22 | 23 | let (ref x, _) = (1, 2); // ok, not top level 24 | println!("The answer is {}.", x); 25 | 26 | // Make sure that allowing the lint works 27 | #[allow(clippy::toplevel_ref_arg)] 28 | let ref mut x = 1_234_543; 29 | } 30 | -------------------------------------------------------------------------------- /tests/ui/trailing_zeros.rs: -------------------------------------------------------------------------------- 1 | #![feature(stmt_expr_attributes)] 2 | #![allow(unused_parens)] 3 | 4 | fn main() { 5 | let x: i32 = 42; 6 | let _ = #[clippy::author] 7 | (x & 0b1111 == 0); // suggest trailing_zeros 8 | let _ = x & 0b1_1111 == 0; // suggest trailing_zeros 9 | let _ = x & 0b1_1010 == 0; // do not lint 10 | let _ = x & 1 == 0; // do not lint 11 | } 12 | -------------------------------------------------------------------------------- /tests/ui/trailing_zeros.stderr: -------------------------------------------------------------------------------- 1 | error: bit mask could be simplified with a call to `trailing_zeros` 2 | --> $DIR/trailing_zeros.rs:7:5 3 | | 4 | LL | (x & 0b1111 == 0); // suggest trailing_zeros 5 | | ^^^^^^^^^^^^^^^^^ help: try: `x.trailing_zeros() >= 4` 6 | | 7 | = note: `-D clippy::verbose-bit-mask` implied by `-D warnings` 8 | 9 | error: bit mask could be simplified with a call to `trailing_zeros` 10 | --> $DIR/trailing_zeros.rs:8:13 11 | | 12 | LL | let _ = x & 0b1_1111 == 0; // suggest trailing_zeros 13 | | ^^^^^^^^^^^^^^^^^ help: try: `x.trailing_zeros() >= 5` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/trailing_zeros.stdout: -------------------------------------------------------------------------------- 1 | if_chain! { 2 | if let ExprKind::Binary(ref op, ref left, ref right) = expr.node; 3 | if BinOpKind::Eq == op.node; 4 | if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node; 5 | if BinOpKind::BitAnd == op1.node; 6 | if let ExprKind::Path(ref path) = left1.node; 7 | if match_qpath(path, &["x"]); 8 | if let ExprKind::Lit(ref lit) = right1.node; 9 | if let LitKind::Int(15, _) = lit.node; 10 | if let ExprKind::Lit(ref lit1) = right.node; 11 | if let LitKind::Int(0, _) = lit1.node; 12 | then { 13 | // report your lint here 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/ui/transmute_32bit.rs: -------------------------------------------------------------------------------- 1 | //ignore-x86_64 2 | 3 | #[warn(wrong_transmute)] 4 | fn main() { 5 | unsafe { 6 | let _: *const usize = std::mem::transmute(6.0f32); 7 | 8 | let _: *mut usize = std::mem::transmute(6.0f32); 9 | 10 | let _: *const usize = std::mem::transmute('x'); 11 | 12 | let _: *mut usize = std::mem::transmute('x'); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/transmute_64bit.rs: -------------------------------------------------------------------------------- 1 | //ignore-x86 2 | //no-ignore-x86_64 3 | 4 | #[warn(clippy::wrong_transmute)] 5 | fn main() { 6 | unsafe { 7 | let _: *const usize = std::mem::transmute(6.0f64); 8 | 9 | let _: *mut usize = std::mem::transmute(6.0f64); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/ui/transmute_64bit.stderr: -------------------------------------------------------------------------------- 1 | error: transmute from a `f64` to a pointer 2 | --> $DIR/transmute_64bit.rs:7:31 3 | | 4 | LL | let _: *const usize = std::mem::transmute(6.0f64); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::wrong-transmute` implied by `-D warnings` 8 | 9 | error: transmute from a `f64` to a pointer 10 | --> $DIR/transmute_64bit.rs:9:29 11 | | 12 | LL | let _: *mut usize = std::mem::transmute(6.0f64); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/transmuting_null.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | #![warn(clippy::transmuting_null)] 3 | #![allow(clippy::zero_ptr)] 4 | #![allow(clippy::transmute_ptr_to_ref)] 5 | #![allow(clippy::eq_op)] 6 | 7 | // Easy to lint because these only span one line. 8 | fn one_liners() { 9 | unsafe { 10 | let _: &u64 = std::mem::transmute(0 as *const u64); 11 | let _: &u64 = std::mem::transmute(std::ptr::null::()); 12 | } 13 | } 14 | 15 | pub const ZPTR: *const usize = 0 as *const _; 16 | pub const NOT_ZPTR: *const usize = 1 as *const _; 17 | 18 | fn transmute_const() { 19 | unsafe { 20 | // Should raise a lint. 21 | let _: &u64 = std::mem::transmute(ZPTR); 22 | // Should NOT raise a lint. 23 | let _: &u64 = std::mem::transmute(NOT_ZPTR); 24 | } 25 | } 26 | 27 | fn main() { 28 | one_liners(); 29 | transmute_const(); 30 | } 31 | -------------------------------------------------------------------------------- /tests/ui/transmuting_null.stderr: -------------------------------------------------------------------------------- 1 | error: transmuting a known null pointer into a reference. 2 | --> $DIR/transmuting_null.rs:10:23 3 | | 4 | LL | let _: &u64 = std::mem::transmute(0 as *const u64); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::transmuting-null` implied by `-D warnings` 8 | 9 | error: transmuting a known null pointer into a reference. 10 | --> $DIR/transmuting_null.rs:11:23 11 | | 12 | LL | let _: &u64 = std::mem::transmute(std::ptr::null::()); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 14 | 15 | error: transmuting a known null pointer into a reference. 16 | --> $DIR/transmuting_null.rs:21:23 17 | | 18 | LL | let _: &u64 = std::mem::transmute(ZPTR); 19 | | ^^^^^^^^^^^^^^^^^^^^^^^^^ 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/ty_fn_sig.rs: -------------------------------------------------------------------------------- 1 | // Regression test 2 | 3 | pub fn retry(f: F) { 4 | for _i in 0.. { 5 | f(); 6 | } 7 | } 8 | 9 | fn main() { 10 | for y in 0..4 { 11 | let func = || (); 12 | func(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/ty_fn_sig.stderr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/ty_fn_sig.stderr -------------------------------------------------------------------------------- /tests/ui/types.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(dead_code, unused_variables)] 4 | 5 | // should not warn on lossy casting in constant types 6 | // because not supported yet 7 | const C: i32 = 42; 8 | const C_I64: i64 = C as i64; 9 | 10 | fn main() { 11 | // should suggest i64::from(c) 12 | let c: i32 = 42; 13 | let c_i64: i64 = i64::from(c); 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/types.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(dead_code, unused_variables)] 4 | 5 | // should not warn on lossy casting in constant types 6 | // because not supported yet 7 | const C: i32 = 42; 8 | const C_I64: i64 = C as i64; 9 | 10 | fn main() { 11 | // should suggest i64::from(c) 12 | let c: i32 = 42; 13 | let c_i64: i64 = c as i64; 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/types.stderr: -------------------------------------------------------------------------------- 1 | error: casting i32 to i64 may become silently lossy if you later change the type 2 | --> $DIR/types.rs:13:22 3 | | 4 | LL | let c_i64: i64 = c as i64; 5 | | ^^^^^^^^ help: try: `i64::from(c)` 6 | | 7 | = note: `-D clippy::cast-lossless` implied by `-D warnings` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /tests/ui/unicode.rs: -------------------------------------------------------------------------------- 1 | #[warn(clippy::zero_width_space)] 2 | fn zero() { 3 | print!("Here >​< is a ZWS, and ​another"); 4 | print!("This\u{200B}is\u{200B}fine"); 5 | } 6 | 7 | #[warn(clippy::unicode_not_nfc)] 8 | fn canon() { 9 | print!("̀àh?"); 10 | print!("a\u{0300}h?"); // also ok 11 | } 12 | 13 | #[warn(clippy::non_ascii_literal)] 14 | fn uni() { 15 | print!("Üben!"); 16 | print!("\u{DC}ben!"); // this is ok 17 | } 18 | 19 | fn main() { 20 | zero(); 21 | uni(); 22 | canon(); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/unicode.stderr: -------------------------------------------------------------------------------- 1 | error: zero-width space detected 2 | --> $DIR/unicode.rs:3:12 3 | | 4 | LL | print!("Here >​< is a ZWS, and ​another"); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::zero-width-space` implied by `-D warnings` 8 | = help: Consider replacing the string with: 9 | ""Here >/u{200B}< is a ZWS, and /u{200B}another"" 10 | 11 | error: non-nfc unicode sequence detected 12 | --> $DIR/unicode.rs:9:12 13 | | 14 | LL | print!("̀àh?"); 15 | | ^^^^^ 16 | | 17 | = note: `-D clippy::unicode-not-nfc` implied by `-D warnings` 18 | = help: Consider replacing the string with: 19 | ""̀àh?"" 20 | 21 | error: literal non-ASCII character detected 22 | --> $DIR/unicode.rs:15:12 23 | | 24 | LL | print!("Üben!"); 25 | | ^^^^^^^ 26 | | 27 | = note: `-D clippy::non-ascii-literal` implied by `-D warnings` 28 | = help: Consider replacing the string with: 29 | ""/u{dc}ben!"" 30 | 31 | error: aborting due to 3 previous errors 32 | 33 | -------------------------------------------------------------------------------- /tests/ui/unit_cmp.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::unit_cmp)] 2 | #![allow(clippy::no_effect, clippy::unnecessary_operation)] 3 | 4 | #[derive(PartialEq)] 5 | pub struct ContainsUnit(()); // should be fine 6 | 7 | fn main() { 8 | // this is fine 9 | if true == false {} 10 | 11 | // this warns 12 | if { 13 | true; 14 | } == { 15 | false; 16 | } {} 17 | 18 | if { 19 | true; 20 | } > { 21 | false; 22 | } {} 23 | } 24 | -------------------------------------------------------------------------------- /tests/ui/unit_cmp.stderr: -------------------------------------------------------------------------------- 1 | error: ==-comparison of unit values detected. This will always be true 2 | --> $DIR/unit_cmp.rs:12:8 3 | | 4 | LL | if { 5 | | ________^ 6 | LL | | true; 7 | LL | | } == { 8 | LL | | false; 9 | LL | | } {} 10 | | |_____^ 11 | | 12 | = note: `-D clippy::unit-cmp` implied by `-D warnings` 13 | 14 | error: >-comparison of unit values detected. This will always be false 15 | --> $DIR/unit_cmp.rs:18:8 16 | | 17 | LL | if { 18 | | ________^ 19 | LL | | true; 20 | LL | | } > { 21 | LL | | false; 22 | LL | | } {} 23 | | |_____^ 24 | 25 | error: aborting due to 2 previous errors 26 | 27 | -------------------------------------------------------------------------------- /tests/ui/unknown_attribute.rs: -------------------------------------------------------------------------------- 1 | #[clippy::unknown] 2 | #[clippy::cognitive_complexity = "1"] 3 | fn main() {} 4 | -------------------------------------------------------------------------------- /tests/ui/unknown_attribute.stderr: -------------------------------------------------------------------------------- 1 | error: Usage of unknown attribute 2 | --> $DIR/unknown_attribute.rs:1:11 3 | | 4 | LL | #[clippy::unknown] 5 | | ^^^^^^^ 6 | 7 | error: aborting due to previous error 8 | 9 | -------------------------------------------------------------------------------- /tests/ui/unknown_clippy_lints.rs: -------------------------------------------------------------------------------- 1 | #![allow(clippy::All)] 2 | #![warn(clippy::pedantic)] 3 | 4 | #[warn(clippy::if_not_els)] 5 | fn main() {} 6 | -------------------------------------------------------------------------------- /tests/ui/unknown_clippy_lints.stderr: -------------------------------------------------------------------------------- 1 | error: unknown clippy lint: clippy::if_not_els 2 | --> $DIR/unknown_clippy_lints.rs:4:8 3 | | 4 | LL | #[warn(clippy::if_not_els)] 5 | | ^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::unknown-clippy-lints` implied by `-D warnings` 8 | 9 | error: unknown clippy lint: clippy::All 10 | --> $DIR/unknown_clippy_lints.rs:1:10 11 | | 12 | LL | #![allow(clippy::All)] 13 | | ^^^^^^^^^^^ help: lowercase the lint name: `all` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/unnecessary_filter_map.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let _ = (0..4).filter_map(|x| if x > 1 { Some(x) } else { None }); 3 | let _ = (0..4).filter_map(|x| { 4 | if x > 1 { 5 | return Some(x); 6 | }; 7 | None 8 | }); 9 | let _ = (0..4).filter_map(|x| match x { 10 | 0 | 1 => None, 11 | _ => Some(x), 12 | }); 13 | 14 | let _ = (0..4).filter_map(|x| Some(x + 1)); 15 | 16 | let _ = (0..4).filter_map(i32::checked_abs); 17 | } 18 | -------------------------------------------------------------------------------- /tests/ui/unnecessary_ref.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![feature(stmt_expr_attributes)] 4 | #![allow(unused_variables)] 5 | 6 | struct Outer { 7 | inner: u32, 8 | } 9 | 10 | #[deny(clippy::ref_in_deref)] 11 | fn main() { 12 | let outer = Outer { inner: 0 }; 13 | let inner = outer.inner; 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/unnecessary_ref.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![feature(stmt_expr_attributes)] 4 | #![allow(unused_variables)] 5 | 6 | struct Outer { 7 | inner: u32, 8 | } 9 | 10 | #[deny(clippy::ref_in_deref)] 11 | fn main() { 12 | let outer = Outer { inner: 0 }; 13 | let inner = (&outer).inner; 14 | } 15 | -------------------------------------------------------------------------------- /tests/ui/unnecessary_ref.stderr: -------------------------------------------------------------------------------- 1 | error: Creating a reference that is immediately dereferenced. 2 | --> $DIR/unnecessary_ref.rs:13:17 3 | | 4 | LL | let inner = (&outer).inner; 5 | | ^^^^^^^^ help: try this: `outer` 6 | | 7 | note: lint level defined here 8 | --> $DIR/unnecessary_ref.rs:10:8 9 | | 10 | LL | #[deny(clippy::ref_in_deref)] 11 | | ^^^^^^^^^^^^^^^^^^^^ 12 | 13 | error: aborting due to previous error 14 | 15 | -------------------------------------------------------------------------------- /tests/ui/unneeded_field_pattern.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::unneeded_field_pattern)] 2 | #[allow(dead_code, unused)] 3 | 4 | struct Foo { 5 | a: i32, 6 | b: i32, 7 | c: i32, 8 | } 9 | 10 | fn main() { 11 | let f = Foo { a: 0, b: 0, c: 0 }; 12 | 13 | match f { 14 | Foo { a: _, b: 0, .. } => {}, 15 | 16 | Foo { a: _, b: _, c: _ } => {}, 17 | } 18 | match f { 19 | Foo { b: 0, .. } => {}, // should be OK 20 | Foo { .. } => {}, // and the Force might be with this one 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/ui/unneeded_field_pattern.stderr: -------------------------------------------------------------------------------- 1 | error: You matched a field with a wildcard pattern. Consider using `..` instead 2 | --> $DIR/unneeded_field_pattern.rs:14:15 3 | | 4 | LL | Foo { a: _, b: 0, .. } => {}, 5 | | ^^^^ 6 | | 7 | = note: `-D clippy::unneeded-field-pattern` implied by `-D warnings` 8 | = help: Try with `Foo { b: 0, .. }` 9 | 10 | error: All the struct fields are matched to a wildcard pattern, consider using `..`. 11 | --> $DIR/unneeded_field_pattern.rs:16:9 12 | | 13 | LL | Foo { a: _, b: _, c: _ } => {}, 14 | | ^^^^^^^^^^^^^^^^^^^^^^^^ 15 | | 16 | = help: Try with `Foo { .. }` instead 17 | 18 | error: aborting due to 2 previous errors 19 | 20 | -------------------------------------------------------------------------------- /tests/ui/unreadable_literal.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #[warn(clippy::unreadable_literal)] 4 | #[allow(unused_variables)] 5 | fn main() { 6 | let good = ( 7 | 0b1011_i64, 8 | 0o1_234_u32, 9 | 0x1_234_567, 10 | 65536, 11 | 1_2345_6789, 12 | 1234_f32, 13 | 1_234.12_f32, 14 | 1_234.123_f32, 15 | 1.123_4_f32, 16 | ); 17 | let bad = (0b11_0110_i64, 0x0123_4567_8901_usize, 123_456_f32, 1.234_567_f32); 18 | let good_sci = 1.1234e1; 19 | let bad_sci = 1.123_456e1; 20 | 21 | let fail9 = 0x00ab_cdef; 22 | let fail10: u32 = 0xBAFE_BAFE; 23 | let fail11 = 0x0abc_deff; 24 | let fail12: i128 = 0x00ab_cabc_abca_bcab_cabc; 25 | } 26 | -------------------------------------------------------------------------------- /tests/ui/unreadable_literal.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #[warn(clippy::unreadable_literal)] 4 | #[allow(unused_variables)] 5 | fn main() { 6 | let good = ( 7 | 0b1011_i64, 8 | 0o1_234_u32, 9 | 0x1_234_567, 10 | 65536, 11 | 1_2345_6789, 12 | 1234_f32, 13 | 1_234.12_f32, 14 | 1_234.123_f32, 15 | 1.123_4_f32, 16 | ); 17 | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32); 18 | let good_sci = 1.1234e1; 19 | let bad_sci = 1.123456e1; 20 | 21 | let fail9 = 0xabcdef; 22 | let fail10: u32 = 0xBAFEBAFE; 23 | let fail11 = 0xabcdeff; 24 | let fail12: i128 = 0xabcabcabcabcabcabc; 25 | } 26 | -------------------------------------------------------------------------------- /tests/ui/unsafe_removed_from_name.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused_imports)] 2 | #![allow(dead_code)] 3 | #![warn(clippy::unsafe_removed_from_name)] 4 | 5 | use std::cell::UnsafeCell as TotallySafeCell; 6 | 7 | use std::cell::UnsafeCell as TotallySafeCellAgain; 8 | 9 | // Shouldn't error 10 | use std::cell::RefCell as ProbablyNotUnsafe; 11 | use std::cell::RefCell as RefCellThatCantBeUnsafe; 12 | use std::cell::UnsafeCell as SuperDangerousUnsafeCell; 13 | use std::cell::UnsafeCell as Dangerunsafe; 14 | use std::cell::UnsafeCell as Bombsawayunsafe; 15 | 16 | mod mod_with_some_unsafe_things { 17 | pub struct Safe {} 18 | pub struct Unsafe {} 19 | } 20 | 21 | use mod_with_some_unsafe_things::Unsafe as LieAboutModSafety; 22 | 23 | // Shouldn't error 24 | use mod_with_some_unsafe_things::Safe as IPromiseItsSafeThisTime; 25 | use mod_with_some_unsafe_things::Unsafe as SuperUnsafeModThing; 26 | 27 | fn main() {} 28 | -------------------------------------------------------------------------------- /tests/ui/unsafe_removed_from_name.stderr: -------------------------------------------------------------------------------- 1 | error: removed "unsafe" from the name of `UnsafeCell` in use as `TotallySafeCell` 2 | --> $DIR/unsafe_removed_from_name.rs:5:1 3 | | 4 | LL | use std::cell::UnsafeCell as TotallySafeCell; 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::unsafe-removed-from-name` implied by `-D warnings` 8 | 9 | error: removed "unsafe" from the name of `UnsafeCell` in use as `TotallySafeCellAgain` 10 | --> $DIR/unsafe_removed_from_name.rs:7:1 11 | | 12 | LL | use std::cell::UnsafeCell as TotallySafeCellAgain; 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 14 | 15 | error: removed "unsafe" from the name of `Unsafe` in use as `LieAboutModSafety` 16 | --> $DIR/unsafe_removed_from_name.rs:21:1 17 | | 18 | LL | use mod_with_some_unsafe_things::Unsafe as LieAboutModSafety; 19 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/unused_io_amount.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | #![warn(clippy::unused_io_amount)] 3 | 4 | use std::io; 5 | 6 | fn try_macro(s: &mut T) -> io::Result<()> { 7 | try!(s.write(b"test")); 8 | let mut buf = [0u8; 4]; 9 | try!(s.read(&mut buf)); 10 | Ok(()) 11 | } 12 | 13 | fn question_mark(s: &mut T) -> io::Result<()> { 14 | s.write(b"test")?; 15 | let mut buf = [0u8; 4]; 16 | s.read(&mut buf)?; 17 | Ok(()) 18 | } 19 | 20 | fn unwrap(s: &mut T) { 21 | s.write(b"test").unwrap(); 22 | let mut buf = [0u8; 4]; 23 | s.read(&mut buf).unwrap(); 24 | } 25 | 26 | fn main() {} 27 | -------------------------------------------------------------------------------- /tests/ui/unused_labels.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code, clippy::items_after_statements, clippy::never_loop)] 2 | #![warn(clippy::unused_label)] 3 | 4 | fn unused_label() { 5 | 'label: for i in 1..2 { 6 | if i > 4 { 7 | continue; 8 | } 9 | } 10 | } 11 | 12 | fn foo() { 13 | 'same_label_in_two_fns: loop { 14 | break 'same_label_in_two_fns; 15 | } 16 | } 17 | 18 | fn bla() { 19 | 'a: loop { 20 | break; 21 | } 22 | fn blub() {} 23 | } 24 | 25 | fn main() { 26 | 'a: for _ in 0..10 { 27 | while let Some(42) = None { 28 | continue 'a; 29 | } 30 | } 31 | 32 | 'same_label_in_two_fns: loop { 33 | let _ = 1; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/ui/unused_labels.stderr: -------------------------------------------------------------------------------- 1 | error: unused label `'label` 2 | --> $DIR/unused_labels.rs:5:5 3 | | 4 | LL | / 'label: for i in 1..2 { 5 | LL | | if i > 4 { 6 | LL | | continue; 7 | LL | | } 8 | LL | | } 9 | | |_____^ 10 | | 11 | = note: `-D clippy::unused-label` implied by `-D warnings` 12 | 13 | error: unused label `'a` 14 | --> $DIR/unused_labels.rs:19:5 15 | | 16 | LL | / 'a: loop { 17 | LL | | break; 18 | LL | | } 19 | | |_____^ 20 | 21 | error: unused label `'same_label_in_two_fns` 22 | --> $DIR/unused_labels.rs:32:5 23 | | 24 | LL | / 'same_label_in_two_fns: loop { 25 | LL | | let _ = 1; 26 | LL | | } 27 | | |_____^ 28 | 29 | error: aborting due to 3 previous errors 30 | 31 | -------------------------------------------------------------------------------- /tests/ui/unused_lt.stderr: -------------------------------------------------------------------------------- 1 | error: this lifetime isn't used in the function definition 2 | --> $DIR/unused_lt.rs:14:14 3 | | 4 | LL | fn unused_lt<'a>(x: u8) {} 5 | | ^^ 6 | | 7 | = note: `-D clippy::extra-unused-lifetimes` implied by `-D warnings` 8 | 9 | error: this lifetime isn't used in the function definition 10 | --> $DIR/unused_lt.rs:16:25 11 | | 12 | LL | fn unused_lt_transitive<'a, 'b: 'a>(x: &'b u8) { 13 | | ^^ 14 | 15 | error: this lifetime isn't used in the function definition 16 | --> $DIR/unused_lt.rs:41:10 17 | | 18 | LL | fn x<'a>(&self) {} 19 | | ^^ 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/unwrap_or.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::all)] 2 | 3 | fn main() { 4 | let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len(); 5 | } 6 | 7 | fn new_lines() { 8 | let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len(); 9 | } 10 | -------------------------------------------------------------------------------- /tests/ui/unwrap_or.stderr: -------------------------------------------------------------------------------- 1 | error: use of `unwrap_or` followed by a function call 2 | --> $DIR/unwrap_or.rs:4:47 3 | | 4 | LL | let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len(); 5 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "Fail".to_string())` 6 | | 7 | = note: `-D clippy::or-fun-call` implied by `-D warnings` 8 | 9 | error: use of `unwrap_or` followed by a function call 10 | --> $DIR/unwrap_or.rs:8:47 11 | | 12 | LL | let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len(); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "Fail".to_string())` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/unwrap_or.stdout: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manishearth/rust-clippy/3710ec59962295336ab4aed100267b584dd7df7d/tests/ui/unwrap_or.stdout -------------------------------------------------------------------------------- /tests/ui/update-all-references.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # A script to update the references for all tests. The idea is that 4 | # you do a run, which will generate files in the build directory 5 | # containing the (normalized) actual output of the compiler. You then 6 | # run this script, which will copy those files over. If you find 7 | # yourself manually editing a foo.stderr file, you're doing it wrong. 8 | # 9 | # See all `update-references.sh`, if you just want to update a single test. 10 | 11 | if [[ "$1" == "--help" || "$1" == "-h" ]]; then 12 | echo "usage: $0" 13 | fi 14 | 15 | BUILD_DIR=$PWD/target/debug/test_build_base 16 | MY_DIR=$(dirname $0) 17 | cd $MY_DIR 18 | find . -name '*.rs' | xargs ./update-references.sh $BUILD_DIR 19 | -------------------------------------------------------------------------------- /tests/ui/useful_asref.rs: -------------------------------------------------------------------------------- 1 | #![deny(clippy::useless_asref)] 2 | 3 | trait Trait { 4 | fn as_ptr(&self); 5 | } 6 | 7 | impl<'a> Trait for &'a [u8] { 8 | fn as_ptr(&self) { 9 | self.as_ref().as_ptr(); 10 | } 11 | } 12 | 13 | fn main() {} 14 | -------------------------------------------------------------------------------- /tests/ui/useless_attribute.rs: -------------------------------------------------------------------------------- 1 | // aux-build:proc_macro_derive.rs 2 | 3 | #![warn(clippy::useless_attribute)] 4 | 5 | #[allow(dead_code)] 6 | #[cfg_attr(feature = "cargo-clippy", allow(dead_code))] 7 | #[rustfmt::skip] 8 | #[cfg_attr(feature = "cargo-clippy", 9 | allow(dead_code))] 10 | #[allow(unused_imports)] 11 | #[allow(unused_extern_crates)] 12 | #[macro_use] 13 | extern crate clippy_lints; 14 | 15 | #[macro_use] 16 | extern crate proc_macro_derive; 17 | 18 | // don't lint on unused_import for `use` items 19 | #[allow(unused_imports)] 20 | use std::collections; 21 | 22 | // don't lint on deprecated for `use` items 23 | mod foo { 24 | #[deprecated] 25 | pub struct Bar; 26 | } 27 | #[allow(deprecated)] 28 | pub use foo::Bar; 29 | 30 | // This should not trigger the lint. There's lint level definitions inside the external derive 31 | // that would trigger the useless_attribute lint. 32 | #[derive(DeriveSomething)] 33 | struct Baz; 34 | 35 | fn main() {} 36 | -------------------------------------------------------------------------------- /tests/ui/useless_attribute.stderr: -------------------------------------------------------------------------------- 1 | error: useless lint attribute 2 | --> $DIR/useless_attribute.rs:5:1 3 | | 4 | LL | #[allow(dead_code)] 5 | | ^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(dead_code)]` 6 | | 7 | = note: `-D clippy::useless-attribute` implied by `-D warnings` 8 | 9 | error: useless lint attribute 10 | --> $DIR/useless_attribute.rs:6:1 11 | | 12 | LL | #[cfg_attr(feature = "cargo-clippy", allow(dead_code))] 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code)` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/vec_box_sized.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(dead_code)] 4 | 5 | struct SizedStruct(i32); 6 | struct UnsizedStruct([i32]); 7 | 8 | /// The following should trigger the lint 9 | mod should_trigger { 10 | use super::SizedStruct; 11 | 12 | struct StructWithVecBox { 13 | sized_type: Vec, 14 | } 15 | 16 | struct A(Vec); 17 | struct B(Vec>); 18 | } 19 | 20 | /// The following should not trigger the lint 21 | mod should_not_trigger { 22 | use super::UnsizedStruct; 23 | 24 | struct C(Vec>); 25 | 26 | struct StructWithVecBoxButItsUnsized { 27 | unsized_type: Vec>, 28 | } 29 | 30 | struct TraitVec { 31 | // Regression test for #3720. This was causing an ICE. 32 | inner: Vec>, 33 | } 34 | } 35 | 36 | fn main() {} 37 | -------------------------------------------------------------------------------- /tests/ui/vec_box_sized.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(dead_code)] 4 | 5 | struct SizedStruct(i32); 6 | struct UnsizedStruct([i32]); 7 | 8 | /// The following should trigger the lint 9 | mod should_trigger { 10 | use super::SizedStruct; 11 | 12 | struct StructWithVecBox { 13 | sized_type: Vec>, 14 | } 15 | 16 | struct A(Vec>); 17 | struct B(Vec>>); 18 | } 19 | 20 | /// The following should not trigger the lint 21 | mod should_not_trigger { 22 | use super::UnsizedStruct; 23 | 24 | struct C(Vec>); 25 | 26 | struct StructWithVecBoxButItsUnsized { 27 | unsized_type: Vec>, 28 | } 29 | 30 | struct TraitVec { 31 | // Regression test for #3720. This was causing an ICE. 32 | inner: Vec>, 33 | } 34 | } 35 | 36 | fn main() {} 37 | -------------------------------------------------------------------------------- /tests/ui/vec_box_sized.stderr: -------------------------------------------------------------------------------- 1 | error: `Vec` is already on the heap, the boxing is unnecessary. 2 | --> $DIR/vec_box_sized.rs:13:21 3 | | 4 | LL | sized_type: Vec>, 5 | | ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec` 6 | | 7 | = note: `-D clippy::vec-box` implied by `-D warnings` 8 | 9 | error: `Vec` is already on the heap, the boxing is unnecessary. 10 | --> $DIR/vec_box_sized.rs:16:14 11 | | 12 | LL | struct A(Vec>); 13 | | ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec` 14 | 15 | error: `Vec` is already on the heap, the boxing is unnecessary. 16 | --> $DIR/vec_box_sized.rs:17:18 17 | | 18 | LL | struct B(Vec>>); 19 | | ^^^^^^^^^^^^^^^ help: try: `Vec` 20 | 21 | error: aborting due to 3 previous errors 22 | 23 | -------------------------------------------------------------------------------- /tests/ui/writeln_empty_string.fixed: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(unused_must_use)] 4 | #![warn(clippy::writeln_empty_string)] 5 | use std::io::Write; 6 | 7 | fn main() { 8 | let mut v = Vec::new(); 9 | 10 | // These should fail 11 | writeln!(&mut v); 12 | 13 | let mut suggestion = Vec::new(); 14 | writeln!(&mut suggestion); 15 | 16 | // These should be fine 17 | writeln!(&mut v); 18 | writeln!(&mut v, " "); 19 | write!(&mut v, ""); 20 | } 21 | -------------------------------------------------------------------------------- /tests/ui/writeln_empty_string.rs: -------------------------------------------------------------------------------- 1 | // run-rustfix 2 | 3 | #![allow(unused_must_use)] 4 | #![warn(clippy::writeln_empty_string)] 5 | use std::io::Write; 6 | 7 | fn main() { 8 | let mut v = Vec::new(); 9 | 10 | // These should fail 11 | writeln!(&mut v, ""); 12 | 13 | let mut suggestion = Vec::new(); 14 | writeln!(&mut suggestion, ""); 15 | 16 | // These should be fine 17 | writeln!(&mut v); 18 | writeln!(&mut v, " "); 19 | write!(&mut v, ""); 20 | } 21 | -------------------------------------------------------------------------------- /tests/ui/writeln_empty_string.stderr: -------------------------------------------------------------------------------- 1 | error: using `writeln!(&mut v, "")` 2 | --> $DIR/writeln_empty_string.rs:11:5 3 | | 4 | LL | writeln!(&mut v, ""); 5 | | ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(&mut v)` 6 | | 7 | = note: `-D clippy::writeln-empty-string` implied by `-D warnings` 8 | 9 | error: using `writeln!(&mut suggestion, "")` 10 | --> $DIR/writeln_empty_string.rs:14:5 11 | | 12 | LL | writeln!(&mut suggestion, ""); 13 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(&mut suggestion)` 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/ui/zero_div_zero.rs: -------------------------------------------------------------------------------- 1 | #[allow(unused_variables)] 2 | #[warn(clippy::zero_divided_by_zero)] 3 | fn main() { 4 | let nan = 0.0 / 0.0; 5 | let f64_nan = 0.0 / 0.0f64; 6 | let other_f64_nan = 0.0f64 / 0.0; 7 | let one_more_f64_nan = 0.0f64 / 0.0f64; 8 | let zero = 0.0; 9 | let other_zero = 0.0; 10 | let other_nan = zero / other_zero; // fine - this lint doesn't propegate constants. 11 | let not_nan = 2.0 / 0.0; // not an error: 2/0 = inf 12 | let also_not_nan = 0.0 / 2.0; // not an error: 0/2 = 0 13 | } 14 | -------------------------------------------------------------------------------- /tests/ui/zero_ptr.rs: -------------------------------------------------------------------------------- 1 | #[allow(unused_variables)] 2 | fn main() { 3 | let x = 0 as *const usize; 4 | let y = 0 as *mut f64; 5 | 6 | let z = 0; 7 | let z = z as *const usize; // this is currently not caught 8 | } 9 | -------------------------------------------------------------------------------- /tests/ui/zero_ptr.stderr: -------------------------------------------------------------------------------- 1 | error: `0 as *const _` detected. Consider using `ptr::null()` 2 | --> $DIR/zero_ptr.rs:3:13 3 | | 4 | LL | let x = 0 as *const usize; 5 | | ^^^^^^^^^^^^^^^^^ 6 | | 7 | = note: `-D clippy::zero-ptr` implied by `-D warnings` 8 | 9 | error: `0 as *mut _` detected. Consider using `ptr::null_mut()` 10 | --> $DIR/zero_ptr.rs:4:13 11 | | 12 | LL | let y = 0 as *mut f64; 13 | | ^^^^^^^^^^^^^ 14 | 15 | error: aborting due to 2 previous errors 16 | 17 | -------------------------------------------------------------------------------- /tests/versioncheck.rs: -------------------------------------------------------------------------------- 1 | #[test] 2 | fn check_that_clippy_lints_has_the_same_version_as_clippy() { 3 | let clippy_meta = cargo_metadata::MetadataCommand::new() 4 | .no_deps() 5 | .exec() 6 | .expect("could not obtain cargo metadata"); 7 | std::env::set_current_dir(std::env::current_dir().unwrap().join("clippy_lints")).unwrap(); 8 | let clippy_lints_meta = cargo_metadata::MetadataCommand::new() 9 | .no_deps() 10 | .exec() 11 | .expect("could not obtain cargo metadata"); 12 | assert_eq!(clippy_lints_meta.packages[0].version, clippy_meta.packages[0].version); 13 | for package in &clippy_meta.packages[0].dependencies { 14 | if package.name == "clippy_lints" { 15 | assert!(package.req.matches(&clippy_lints_meta.packages[0].version)); 16 | return; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /util/dev: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd clippy_dev && cargo run -- $@ 4 | -------------------------------------------------------------------------------- /util/fetch_prs_between.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Fetches the merge commits between two git commits and prints the PR URL 4 | # together with the full commit message 5 | # 6 | # If you want to use this to update the Clippy changelog, be sure to manually 7 | # exclude the non-user facing changes like 'rustup' PRs, typo fixes, etc. 8 | 9 | first=$1 10 | last=$2 11 | 12 | IFS=' 13 | ' 14 | for pr in $(git log --oneline --grep "Merge #" --grep "Merge pull request" --grep "Auto merge of" "$first...$last" | sort -rn | uniq); do 15 | id=$(echo $pr | rg -o '#[0-9]{3,5}' | cut -c 2-) 16 | commit=$(echo $pr | cut -d' ' -f 1) 17 | echo "URL: https://github.com/rust-lang/rust-clippy/pull/$id" 18 | echo "$(git --no-pager show --pretty=medium $commit)" 19 | echo "---------------------------------------------------------\n" 20 | done 21 | -------------------------------------------------------------------------------- /util/update_lints.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | 5 | def main(): 6 | print('Error: Please use `util/dev` to update lints') 7 | return 1 8 | 9 | if __name__ == '__main__': 10 | sys.exit(main()) 11 | --------------------------------------------------------------------------------