Skip to content

Commit b9222e5

Browse files
committed
Updates Semgrep rule setfield-without-assign to support assigning a new context
1 parent 7c5c9d8 commit b9222e5

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

.ci/semgrep/tflog/tflog.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-log/tflog"
7+
)
8+
9+
func noAssignment() {
10+
ctx := context.Background()
11+
12+
// ruleid: setfield-without-assign
13+
tflog.SetField(ctx, "field", "value")
14+
}
15+
16+
func assigned() {
17+
ctx := context.Background()
18+
19+
// ok: setfield-without-assign
20+
ctx = tflog.SetField(ctx, "field", "value")
21+
}
22+
23+
func returnedContext() context.Context {
24+
ctx := context.Background()
25+
26+
// ok: setfield-without-assign
27+
return tflog.SetField(ctx, "field", "value")
28+
}
29+
30+
func declareAndAssign_SameName() {
31+
ctx := context.Background()
32+
33+
for i := 0; i < 1; i++ {
34+
// ok: setfield-without-assign
35+
ctx := tflog.SetField(ctx, "field", "value")
36+
}
37+
}
38+
39+
func declareAndAssign_Rename() {
40+
outerCtx := context.Background()
41+
42+
for i := 0; i < 1; i++ {
43+
// ok: setfield-without-assign
44+
innerCtx := tflog.SetField(outerCtx, "field", "value")
45+
}
46+
}

.ci/semgrep/tflog/tflog.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ rules:
44
message: The return value of "tflog.SetField" must be used
55
patterns:
66
- pattern: tflog.SetField(...)
7-
- pattern-not-inside: $CTX = tflog.SetField($CTX, ...)
7+
- pattern-not-inside: $CTX1 = tflog.SetField($CTX2, ...)
88
- pattern-not-inside: return tflog.SetField($CTX, ...)
99
severity: ERROR

0 commit comments

Comments
 (0)