@@ -452,8 +452,15 @@ func stringConstantArg(pass *analysis.Pass, call *ast.CallExpr, idx int) (string
452
452
if idx >= len (call .Args ) {
453
453
return "" , false
454
454
}
455
- arg := call .Args [idx ]
456
- lit := pass .TypesInfo .Types [arg ].Value
455
+ return stringConstantExpr (pass , call .Args [idx ])
456
+ }
457
+
458
+ // stringConstantExpr returns expression's string constant value.
459
+ //
460
+ // ("", false) is returned if expression isn't a string
461
+ // constant.
462
+ func stringConstantExpr (pass * analysis.Pass , expr ast.Expr ) (string , bool ) {
463
+ lit := pass .TypesInfo .Types [expr ].Value
457
464
if lit != nil && lit .Kind () == constant .String {
458
465
return constant .StringVal (lit ), true
459
466
}
@@ -1053,10 +1060,10 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) {
1053
1060
}
1054
1061
1055
1062
arg := args [0 ]
1056
- if lit , ok := arg .( * ast. BasicLit ); ok && lit . Kind == token . STRING {
1057
- // Ignore trailing % character in lit.Value.
1063
+ if s , ok := stringConstantExpr ( pass , arg ); ok {
1064
+ // Ignore trailing % character
1058
1065
// The % in "abc 0.0%" couldn't be a formatting directive.
1059
- s : = strings .TrimSuffix (lit . Value , `%"` )
1066
+ s = strings .TrimSuffix (s , "%" )
1060
1067
if strings .Contains (s , "%" ) {
1061
1068
m := printFormatRE .FindStringSubmatch (s )
1062
1069
if m != nil {
@@ -1067,9 +1074,8 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) {
1067
1074
if strings .HasSuffix (fn .Name (), "ln" ) {
1068
1075
// The last item, if a string, should not have a newline.
1069
1076
arg = args [len (args )- 1 ]
1070
- if lit , ok := arg .(* ast.BasicLit ); ok && lit .Kind == token .STRING {
1071
- str , _ := strconv .Unquote (lit .Value )
1072
- if strings .HasSuffix (str , "\n " ) {
1077
+ if s , ok := stringConstantExpr (pass , arg ); ok {
1078
+ if strings .HasSuffix (s , "\n " ) {
1073
1079
pass .ReportRangef (call , "%s arg list ends with redundant newline" , fn .FullName ())
1074
1080
}
1075
1081
}
0 commit comments