@@ -239,15 +239,19 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
239239 }
240240
241241 msgPos := funcInfo .argsPos - 1
242+
242243 // NOTE: "With" functions have no message argument and must be skipped.
243244 if opts .StaticMsg && msgPos >= 0 && ! isStaticMsg (call .Args [msgPos ]) {
244245 pass .Reportf (call .Pos (), "message should be a string literal or a constant" )
245246 }
246247
247248 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 {
251255 pass .Reportf (call .Pos (), "message should be %s" , opts .MsgStyle )
252256 }
253257 }
@@ -267,7 +271,6 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
267271 if typ == nil {
268272 continue
269273 }
270-
271274 switch typ .String () {
272275 case "string" :
273276 keys = append (keys , args [i ])
@@ -376,6 +379,11 @@ func isStaticMsg(msg ast.Expr) bool {
376379 return msg .Kind == token .STRING
377380 case * ast.Ident : // e.g. const msg = "msg"; slog.Info(msg)
378381 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 )
379387 default :
380388 return false
381389 }
@@ -455,10 +463,9 @@ func getKeyName(key ast.Expr) (string, bool) {
455463 }
456464 }
457465 if lit , ok := key .(* ast.BasicLit ); ok && lit .Kind == token .STRING {
458- // string literals are always quoted.
459466 value , err := strconv .Unquote (lit .Value )
460467 if err != nil {
461- panic ("unreachable" )
468+ panic ("unreachable" ) // string literals are always quoted.
462469 }
463470 return value , true
464471 }
0 commit comments