@@ -1148,26 +1148,46 @@ fn lint_or_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: Spa
11481148
11491149/// Checks for the `EXPECT_FUN_CALL` lint.
11501150fn lint_expect_fun_call ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , method_span : Span , name : & str , args : & [ hir:: Expr ] ) {
1151- fn extract_format_args ( arg : & hir:: Expr ) -> Option < ( & hir:: Expr , & hir:: Expr ) > {
1152- let arg = match & arg. node {
1153- hir:: ExprKind :: AddrOf ( _, expr) => expr,
1154- hir:: ExprKind :: MethodCall ( method_name, _, args)
1155- if method_name. ident . name == "as_str" || method_name. ident . name == "as_ref" =>
1156- {
1157- & args[ 0 ]
1158- } ,
1159- _ => arg,
1160- } ;
1161-
1162- if let hir:: ExprKind :: Call ( ref inner_fun, ref inner_args) = arg. node {
1163- if is_expn_of ( inner_fun. span , "format" ) . is_some ( ) && inner_args. len ( ) == 1 {
1164- if let hir:: ExprKind :: Call ( _, format_args) = & inner_args[ 0 ] . node {
1165- return Some ( ( & format_args[ 0 ] , & format_args[ 1 ] ) ) ;
1166- }
1167- }
1151+ // Strip `&`, `as_ref()` and `as_str()` off `arg` until we're left with either a `String` or
1152+ // `&str`
1153+ fn get_arg_root < ' a > ( cx : & LateContext < ' _ , ' _ > , arg : & ' a hir:: Expr ) -> & ' a hir:: Expr {
1154+ let mut arg_root = arg;
1155+ loop {
1156+ arg_root = match & arg_root. node {
1157+ hir:: ExprKind :: AddrOf ( _, expr) => expr,
1158+ hir:: ExprKind :: MethodCall ( method_name, _, call_args) => {
1159+ if call_args. len ( ) == 1
1160+ && ( method_name. ident . name == "as_str" || method_name. ident . name == "as_ref" )
1161+ && {
1162+ let arg_type = cx. tables . expr_ty ( & call_args[ 0 ] ) ;
1163+ let base_type = walk_ptrs_ty ( arg_type) ;
1164+ base_type. sty == ty:: Str || match_type ( cx, base_type, & paths:: STRING )
1165+ }
1166+ {
1167+ & call_args[ 0 ]
1168+ } else {
1169+ break ;
1170+ }
1171+ } ,
1172+ _ => break ,
1173+ } ;
11681174 }
1175+ arg_root
1176+ }
11691177
1170- None
1178+ // Only `&'static str` or `String` can be used directly in the `panic!`. Other types should be
1179+ // converted to string.
1180+ fn requires_to_string ( cx : & LateContext < ' _ , ' _ > , arg : & hir:: Expr ) -> bool {
1181+ let arg_ty = cx. tables . expr_ty ( arg) ;
1182+ if match_type ( cx, arg_ty, & paths:: STRING ) {
1183+ return false ;
1184+ }
1185+ if let ty:: Ref ( ty:: ReStatic , ty, ..) = arg_ty. sty {
1186+ if ty. sty == ty:: Str {
1187+ return false ;
1188+ }
1189+ } ;
1190+ true
11711191 }
11721192
11731193 fn generate_format_arg_snippet (
@@ -1189,93 +1209,82 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span:
11891209 unreachable ! ( )
11901210 }
11911211
1192- fn check_general_case (
1193- cx : & LateContext < ' _ , ' _ > ,
1194- name : & str ,
1195- method_span : Span ,
1196- self_expr : & hir:: Expr ,
1197- arg : & hir:: Expr ,
1198- span : Span ,
1199- ) {
1200- fn is_call ( node : & hir:: ExprKind ) -> bool {
1201- match node {
1202- hir:: ExprKind :: AddrOf ( _, expr) => {
1203- is_call ( & expr. node )
1204- } ,
1205- hir:: ExprKind :: Call ( ..)
1206- | hir:: ExprKind :: MethodCall ( ..)
1207- // These variants are debatable or require further examination
1208- | hir:: ExprKind :: If ( ..)
1209- | hir:: ExprKind :: Match ( ..)
1210- | hir:: ExprKind :: Block { .. } => true ,
1211- _ => false ,
1212- }
1213- }
1214-
1215- if name != "expect" {
1216- return ;
1212+ fn is_call ( node : & hir:: ExprKind ) -> bool {
1213+ match node {
1214+ hir:: ExprKind :: AddrOf ( _, expr) => {
1215+ is_call ( & expr. node )
1216+ } ,
1217+ hir:: ExprKind :: Call ( ..)
1218+ | hir:: ExprKind :: MethodCall ( ..)
1219+ // These variants are debatable or require further examination
1220+ | hir:: ExprKind :: If ( ..)
1221+ | hir:: ExprKind :: Match ( ..)
1222+ | hir:: ExprKind :: Block { .. } => true ,
1223+ _ => false ,
12171224 }
1225+ }
12181226
1219- let self_type = cx. tables . expr_ty ( self_expr) ;
1220- let known_types = & [ & paths:: OPTION , & paths:: RESULT ] ;
1227+ if args. len ( ) != 2 || name != "expect" || !is_call ( & args[ 1 ] . node ) {
1228+ return ;
1229+ }
12211230
1222- // if not a known type, return early
1223- if known_types. iter ( ) . all ( |& k| !match_type ( cx, self_type, k) ) {
1224- return ;
1225- }
1231+ let receiver_type = cx. tables . expr_ty ( & args[ 0 ] ) ;
1232+ let closure_args = if match_type ( cx, receiver_type, & paths:: OPTION ) {
1233+ "||"
1234+ } else if match_type ( cx, receiver_type, & paths:: RESULT ) {
1235+ "|_|"
1236+ } else {
1237+ return ;
1238+ } ;
12261239
1227- if !is_call ( & arg. node ) {
1228- return ;
1229- }
1240+ let arg_root = get_arg_root ( cx, & args[ 1 ] ) ;
12301241
1231- let closure = if match_type ( cx, self_type, & paths:: OPTION ) {
1232- "||"
1233- } else {
1234- "|_|"
1235- } ;
1236- let span_replace_word = method_span. with_hi ( span. hi ( ) ) ;
1242+ let span_replace_word = method_span. with_hi ( expr. span . hi ( ) ) ;
12371243
1238- if let Some ( ( fmt_spec, fmt_args) ) = extract_format_args ( arg) {
1239- let mut applicability = Applicability :: MachineApplicable ;
1240- let mut args = vec ! [ snippet( cx, fmt_spec. span, ".." ) . into_owned( ) ] ;
1244+ let mut applicability = Applicability :: MachineApplicable ;
12411245
1242- args. extend ( generate_format_arg_snippet ( cx, fmt_args, & mut applicability) ) ;
1246+ //Special handling for `format!` as arg_root
1247+ if let hir:: ExprKind :: Call ( ref inner_fun, ref inner_args) = arg_root. node {
1248+ if is_expn_of ( inner_fun. span , "format" ) . is_some ( ) && inner_args. len ( ) == 1 {
1249+ if let hir:: ExprKind :: Call ( _, format_args) = & inner_args[ 0 ] . node {
1250+ let fmt_spec = & format_args[ 0 ] ;
1251+ let fmt_args = & format_args[ 1 ] ;
12431252
1244- let sugg = args . join ( ", " ) ;
1253+ let mut args = vec ! [ snippet ( cx , fmt_spec . span , ".." ) . into_owned ( ) ] ;
12451254
1246- span_lint_and_sugg (
1247- cx,
1248- EXPECT_FUN_CALL ,
1249- span_replace_word,
1250- & format ! ( "use of `{}` followed by a function call" , name) ,
1251- "try this" ,
1252- format ! ( "unwrap_or_else({} panic!({}))" , closure, sugg) ,
1253- applicability,
1254- ) ;
1255+ args. extend ( generate_format_arg_snippet ( cx, fmt_args, & mut applicability) ) ;
12551256
1256- return ;
1257- }
1257+ let sugg = args. join ( ", " ) ;
12581258
1259- let mut applicability = Applicability :: MachineApplicable ;
1260- let sugg: Cow < ' _ , _ > = snippet_with_applicability ( cx, arg. span , ".." , & mut applicability) ;
1259+ span_lint_and_sugg (
1260+ cx,
1261+ EXPECT_FUN_CALL ,
1262+ span_replace_word,
1263+ & format ! ( "use of `{}` followed by a function call" , name) ,
1264+ "try this" ,
1265+ format ! ( "unwrap_or_else({} panic!({}))" , closure_args, sugg) ,
1266+ applicability,
1267+ ) ;
12611268
1262- span_lint_and_sugg (
1263- cx,
1264- EXPECT_FUN_CALL ,
1265- span_replace_word,
1266- & format ! ( "use of `{}` followed by a function call" , name) ,
1267- "try this" ,
1268- format ! ( "unwrap_or_else({} {{ let msg = {}; panic!(msg) }}))" , closure, sugg) ,
1269- applicability,
1270- ) ;
1269+ return ;
1270+ }
1271+ }
12711272 }
12721273
1273- if args. len ( ) == 2 {
1274- match args[ 1 ] . node {
1275- hir:: ExprKind :: Lit ( _) => { } ,
1276- _ => check_general_case ( cx, name, method_span, & args[ 0 ] , & args[ 1 ] , expr. span ) ,
1277- }
1274+ let mut arg_root_snippet: Cow < ' _ , _ > = snippet_with_applicability ( cx, arg_root. span , ".." , & mut applicability) ;
1275+ if requires_to_string ( cx, arg_root) {
1276+ arg_root_snippet. to_mut ( ) . push_str ( ".to_string()" ) ;
12781277 }
1278+
1279+ span_lint_and_sugg (
1280+ cx,
1281+ EXPECT_FUN_CALL ,
1282+ span_replace_word,
1283+ & format ! ( "use of `{}` followed by a function call" , name) ,
1284+ "try this" ,
1285+ format ! ( "unwrap_or_else({} {{ panic!({}) }})" , closure_args, arg_root_snippet) ,
1286+ applicability,
1287+ ) ;
12791288}
12801289
12811290/// Checks for the `CLONE_ON_COPY` lint.
0 commit comments