@@ -26,8 +26,9 @@ use crate::consts::{constant, Constant};
26
26
use crate :: utils:: paths;
27
27
use crate :: utils:: {
28
28
clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, last_path_segment, match_def_path,
29
- match_path, multispan_sugg, qpath_res, same_tys, sext, snippet, snippet_opt, snippet_with_applicability,
30
- snippet_with_macro_callsite, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext,
29
+ match_path, method_chain_args, multispan_sugg, qpath_res, same_tys, sext, snippet, snippet_opt,
30
+ snippet_with_applicability, snippet_with_macro_callsite, span_help_and_lint, span_lint, span_lint_and_sugg,
31
+ span_lint_and_then, unsext,
31
32
} ;
32
33
33
34
declare_clippy_lint ! {
@@ -1021,14 +1022,22 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro
1021
1022
}
1022
1023
}
1023
1024
1024
- // don't lint for the result of `abs`
1025
- // `abs` is an inherent impl of `i{N}`, so a method call with ident `abs` will always
1026
- // resolve to that spesific method
1027
- if_chain ! {
1028
- if let ExprKind :: MethodCall ( ref path, _, _) = op. kind;
1029
- if path. ident. name. as_str( ) == "abs" ;
1030
- then {
1031
- return
1025
+ // don't lint for the result of methods that always return non-negative values
1026
+ if let ExprKind :: MethodCall ( ref path, _, _) = op. kind {
1027
+ let mut method_name = path. ident . name . as_str ( ) ;
1028
+ let whitelisted_methods = [ "abs" , "checked_abs" , "rem_euclid" , "checked_rem_euclid" ] ;
1029
+
1030
+ if_chain ! {
1031
+ if method_name == "unwrap" ;
1032
+ if let Some ( arglist) = method_chain_args( op, & [ "unwrap" ] ) ;
1033
+ if let ExprKind :: MethodCall ( ref inner_path, _, _) = & arglist[ 0 ] [ 0 ] . kind;
1034
+ then {
1035
+ method_name = inner_path. ident. name. as_str( ) ;
1036
+ }
1037
+ }
1038
+
1039
+ if whitelisted_methods. iter ( ) . any ( |& name| method_name == name) {
1040
+ return ;
1032
1041
}
1033
1042
}
1034
1043
0 commit comments