@@ -239,15 +239,19 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
239
239
}
240
240
241
241
msgPos := funcInfo .argsPos - 1
242
+
242
243
// NOTE: "With" functions have no message argument and must be skipped.
243
244
if opts .StaticMsg && msgPos >= 0 && ! isStaticMsg (call .Args [msgPos ]) {
244
245
pass .Reportf (call .Pos (), "message should be a string literal or a constant" )
245
246
}
246
247
247
248
if opts .MsgStyle != "" && msgPos >= 0 {
248
- if msg , ok := call .Args [msgPos ].(* ast.BasicLit ); ok && msg .Kind == token .STRING {
249
- msg .Value = msg .Value [1 : len (msg .Value )- 1 ] // trim quotes/backticks.
250
- if ok := isValidMsgStyle (msg .Value , opts .MsgStyle ); ! ok {
249
+ if lit , ok := call .Args [msgPos ].(* ast.BasicLit ); ok && lit .Kind == token .STRING {
250
+ value , err := strconv .Unquote (lit .Value )
251
+ if err != nil {
252
+ panic ("unreachable" ) // string literals are always quoted.
253
+ }
254
+ if ok := isValidMsgStyle (value , opts .MsgStyle ); ! ok {
251
255
pass .Reportf (call .Pos (), "message should be %s" , opts .MsgStyle )
252
256
}
253
257
}
@@ -267,7 +271,6 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
267
271
if typ == nil {
268
272
continue
269
273
}
270
-
271
274
switch typ .String () {
272
275
case "string" :
273
276
keys = append (keys , args [i ])
@@ -376,6 +379,11 @@ func isStaticMsg(msg ast.Expr) bool {
376
379
return msg .Kind == token .STRING
377
380
case * ast.Ident : // e.g. const msg = "msg"; slog.Info(msg)
378
381
return msg .Obj != nil && msg .Obj .Kind == ast .Con
382
+ case * ast.BinaryExpr : // e.g. slog.Info("x" + "y")
383
+ if msg .Op != token .ADD {
384
+ panic ("unreachable" ) // only + can be applied to strings.
385
+ }
386
+ return isStaticMsg (msg .X ) && isStaticMsg (msg .Y )
379
387
default :
380
388
return false
381
389
}
@@ -455,10 +463,9 @@ func getKeyName(key ast.Expr) (string, bool) {
455
463
}
456
464
}
457
465
if lit , ok := key .(* ast.BasicLit ); ok && lit .Kind == token .STRING {
458
- // string literals are always quoted.
459
466
value , err := strconv .Unquote (lit .Value )
460
467
if err != nil {
461
- panic ("unreachable" )
468
+ panic ("unreachable" ) // string literals are always quoted.
462
469
}
463
470
return value , true
464
471
}
0 commit comments