Utilities/ast-grep: add rules for cmHas{Suf,Pre}fix for character needles

This commit is contained in:
Ben Boeckel
2025-10-27 16:15:19 -04:00
parent aa477d8768
commit d9ee22f331
8 changed files with 190 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
id: cmhasprefix-char
snapshots:
cmHasPrefix(haystack, "'"):
fixed: cmHasPrefix(haystack, '\'')
labels:
- source: '"''"'
style: primary
start: 22
end: 25
- source: cmHasPrefix(haystack, "'")
style: secondary
start: 0
end: 26
- source: ','
style: secondary
start: 20
end: 21
cmHasPrefix(haystack, "\n"):
fixed: cmHasPrefix(haystack, '\n')
labels:
- source: '"\n"'
style: primary
start: 22
end: 26
- source: cmHasPrefix(haystack, "\n")
style: secondary
start: 0
end: 27
- source: ','
style: secondary
start: 20
end: 21
cmHasPrefix(haystack, "a"):
fixed: cmHasPrefix(haystack, 'a')
labels:
- source: '"a"'
style: primary
start: 22
end: 25
- source: cmHasPrefix(haystack, "a")
style: secondary
start: 0
end: 26
- source: ','
style: secondary
start: 20
end: 21

View File

@@ -0,0 +1,47 @@
id: cmhassuffix-char
snapshots:
cmHasSuffix(haystack, "'"):
fixed: cmHasSuffix(haystack, '\'')
labels:
- source: '"''"'
style: primary
start: 22
end: 25
- source: cmHasSuffix(haystack, "'")
style: secondary
start: 0
end: 26
- source: ','
style: secondary
start: 20
end: 21
cmHasSuffix(haystack, "\n"):
fixed: cmHasSuffix(haystack, '\n')
labels:
- source: '"\n"'
style: primary
start: 22
end: 26
- source: cmHasSuffix(haystack, "\n")
style: secondary
start: 0
end: 27
- source: ','
style: secondary
start: 20
end: 21
cmHasSuffix(haystack, "a"):
fixed: cmHasSuffix(haystack, 'a')
labels:
- source: '"a"'
style: primary
start: 22
end: 25
- source: cmHasSuffix(haystack, "a")
style: secondary
start: 0
end: 26
- source: ','
style: secondary
start: 20
end: 21

View File

@@ -0,0 +1,9 @@
---
id: cmhasprefix-char
valid:
- "cmHasPrefix(haystack, 'a')"
- 'cmHasPrefix(haystack, "foo")'
invalid:
- 'cmHasPrefix(haystack, "a")'
- 'cmHasPrefix(haystack, "\n")'
- "cmHasPrefix(haystack, \"'\")"

View File

@@ -0,0 +1,9 @@
---
id: cmhassuffix-char
valid:
- "cmHasSuffix(haystack, 'a')"
- 'cmHasSuffix(haystack, "foo")'
invalid:
- 'cmHasSuffix(haystack, "a")'
- 'cmHasSuffix(haystack, "\n")'
- "cmHasSuffix(haystack, \"'\")"

View File

@@ -0,0 +1,34 @@
---
id: cmhasprefix-char
language: Cpp
severity: warning
message: "`cmHasPrefix` with a one-char prefix search should use `cmHasPrefix`"
rule:
kind: string_literal
pattern: $ARG
follows:
regex: '(,|[(])'
inside:
matches: cmhasprefix-call
stopBy:
kind: call_expression
constraints:
ARG:
regex: '^"(.|\\.)"$'
transform:
ARG_CHANGE_QUOTE:
replace:
source: $ARG
replace: '(^"|"$)'
by: "'"
ARG_ESCAPE_SINGLE_QUOTE:
replace:
source: $ARG_CHANGE_QUOTE
replace: "'''"
by: "'\\''"
ARG_OUT:
replace:
source: $ARG_ESCAPE_SINGLE_QUOTE
replace: '\\"'
by: '"'
fix: $ARG_OUT

View File

@@ -0,0 +1,34 @@
---
id: cmhassuffix-char
language: Cpp
severity: warning
message: "`cmHasSuffix` with a one-char suffix search should use `cmHasSuffix`"
rule:
kind: string_literal
pattern: $ARG
follows:
regex: '(,|[(])'
inside:
matches: cmhassuffix-call
stopBy:
kind: call_expression
constraints:
ARG:
regex: '^"(.|\\.)"$'
transform:
ARG_CHANGE_QUOTE:
replace:
source: $ARG
replace: '(^"|"$)'
by: "'"
ARG_ESCAPE_SINGLE_QUOTE:
replace:
source: $ARG_CHANGE_QUOTE
replace: "'''"
by: "'\\''"
ARG_OUT:
replace:
source: $ARG_ESCAPE_SINGLE_QUOTE
replace: '\\"'
by: '"'
fix: $ARG_OUT

View File

@@ -0,0 +1,5 @@
---
id: cmhasprefix-call
language: Cpp
rule:
pattern: cmHasPrefix($$$)

View File

@@ -0,0 +1,5 @@
---
id: cmhassuffix-call
language: Cpp
rule:
pattern: cmHasSuffix($$$)