@@ -47,63 +47,63 @@ class NSString : NSObject {
4747
4848func tests( input: String ) throws {
4949 _ = try Regex ( " ^a| " ) . firstMatch ( in: input)
50- _ = try Regex ( " ^a|b " ) . firstMatch ( in: input) // BAD (missing anchor)
50+ _ = try Regex ( " ^a|b " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
5151 _ = try Regex ( " a|^b " ) . firstMatch ( in: input)
5252 _ = try Regex ( " ^a|^b " ) . firstMatch ( in: input)
53- _ = try Regex ( " ^a|b|c " ) . firstMatch ( in: input) // BAD (missing anchor)
53+ _ = try Regex ( " ^a|b|c " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
5454 _ = try Regex ( " a|^b|c " ) . firstMatch ( in: input)
5555 _ = try Regex ( " a|b|^c " ) . firstMatch ( in: input)
5656 _ = try Regex ( " ^a|^b|c " ) . firstMatch ( in: input)
5757
5858 _ = try Regex ( " (^a)|b " ) . firstMatch ( in: input)
59- _ = try Regex ( " ^a|(b) " ) . firstMatch ( in: input) // BAD (missing anchor)
59+ _ = try Regex ( " ^a|(b) " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
6060 _ = try Regex ( " ^a|(^b) " ) . firstMatch ( in: input)
61- _ = try Regex ( " ^(a)|(b) " ) . firstMatch ( in: input) // BAD (missing anchor)
61+ _ = try Regex ( " ^(a)|(b) " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
6262
63- _ = try Regex ( " a|b$ " ) . firstMatch ( in: input) // BAD (missing anchor)
63+ _ = try Regex ( " a|b$ " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
6464 _ = try Regex ( " a$|b " ) . firstMatch ( in: input)
6565 _ = try Regex ( " a$|b$ " ) . firstMatch ( in: input)
66- _ = try Regex ( " a|b|c$ " ) . firstMatch ( in: input) // BAD (missing anchor)
66+ _ = try Regex ( " a|b|c$ " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
6767 _ = try Regex ( " a|b$|c " ) . firstMatch ( in: input)
6868 _ = try Regex ( " a$|b|c " ) . firstMatch ( in: input)
6969 _ = try Regex ( " a|b$|c$ " ) . firstMatch ( in: input)
7070
7171 _ = try Regex ( " a|(b$) " ) . firstMatch ( in: input)
72- _ = try Regex ( " (a)|b$ " ) . firstMatch ( in: input) // BAD (missing anchor)
72+ _ = try Regex ( " (a)|b$ " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
7373 _ = try Regex ( " (a$)|b$ " ) . firstMatch ( in: input)
74- _ = try Regex ( " (a)|(b)$ " ) . firstMatch ( in: input) // BAD (missing anchor)
74+ _ = try Regex ( " (a)|(b)$ " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
7575
76- _ = try Regex ( #"^good.com|better.com"# ) . firstMatch ( in: input) // BAD (missing anchor)
77- _ = try Regex ( #"^good\.com|better\.com"# ) . firstMatch ( in: input) // BAD (missing anchor)
78- _ = try Regex ( #"^good\\.com|better\\.com"# ) . firstMatch ( in: input) // BAD (missing anchor)
79- _ = try Regex ( #"^good\\\.com|better\\\.com"# ) . firstMatch ( in: input) // BAD (missing anchor)
80- _ = try Regex ( #"^good\\\\.com|better\\\\.com"# ) . firstMatch ( in: input) // BAD (missing anchor)
76+ _ = try Regex ( #"^good.com|better.com"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
77+ _ = try Regex ( #"^good\.com|better\.com"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
78+ _ = try Regex ( #"^good\\.com|better\\.com"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
79+ _ = try Regex ( #"^good\\\.com|better\\\.com"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
80+ _ = try Regex ( #"^good\\\\.com|better\\\\.com"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
8181
82- _ = try Regex ( " ^foo|bar|baz$ " ) . firstMatch ( in: input) // BAD (missing anchor)
82+ _ = try Regex ( " ^foo|bar|baz$ " ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
8383 _ = try Regex ( " ^foo|% " ) . firstMatch ( in: input)
8484}
8585
8686func realWorld( input: String ) throws {
8787 // real-world examples that have been anonymized a bit
8888 // the following are bad:
89- _ = try Regex ( #"(\.xxx)|(\.yyy)|(\.zzz)$"# ) . firstMatch ( in: input) // BAD (missing anchor)
90- _ = try Regex ( #"(^left|right|center)\sbottom$"# ) . firstMatch ( in: input) // BAD (missing anchor)
91- _ = try Regex ( #"\.xxx|\.yyy|\.zzz$"# ) . ignoresCase ( ) . firstMatch ( in: input) // BAD (missing anchor)
92- _ = try Regex ( #"\.xxx|\.yyy|\.zzz$"# ) . ignoresCase ( ) . firstMatch ( in: input) // BAD (missing anchor)
93- _ = try Regex ( #"\.xxx|\.yyy|zzz$"# ) . firstMatch ( in: input) // BAD (missing anchor)
94- _ = try Regex ( #"^([A-Z]|xxx[XY]$)"# ) . firstMatch ( in: input) // BAD (missing anchor)
95- _ = try Regex ( #"^(xxx yyy zzz)|(xxx yyy)"# ) . ignoresCase ( ) . firstMatch ( in: input) // BAD (missing anchor)
96- _ = try Regex ( #"^(xxx yyy zzz)|(xxx yyy)|(1st( xxx)? yyy)|xxx|1st"# ) . ignoresCase ( ) . firstMatch ( in: input) // BAD (missing anchor)
97- _ = try Regex ( #"^(xxx:)|(yyy:)|(zzz:)"# ) . firstMatch ( in: input) // BAD (missing anchor)
98- _ = try Regex ( #"^(xxx?:)|(yyy:zzz\/)"# ) . firstMatch ( in: input) // BAD (missing anchor)
99- _ = try Regex ( #"^@media|@page"# ) . firstMatch ( in: input) // BAD (missing anchor)
100- _ = try Regex ( #"^\s*(xxx?|yyy|zzz):|xxx:yyy"# ) . firstMatch ( in: input) // BAD (missing anchor)
101- _ = try Regex ( #"^click|mouse|touch"# ) . firstMatch ( in: input) // BAD (missing anchor)
102- _ = try Regex ( #"^http://good\.com|http://better\.com"# ) . firstMatch ( in: input) // BAD (missing anchor)
103- _ = try Regex ( #"^https?://good\.com|https?://better\.com"# ) . firstMatch ( in: input) // BAD (missing anchor)
104- _ = try Regex ( #"^mouse|touch|click|contextmenu|drop|dragover|dragend"# ) . firstMatch ( in: input) // BAD (missing anchor)
105- _ = try Regex ( #"^xxx:|yyy:"# ) . ignoresCase ( ) . firstMatch ( in: input) // BAD (missing anchor)
106- _ = try Regex ( #"_xxx|_yyy|_zzz$"# ) . firstMatch ( in: input) // BAD (missing anchor)
89+ _ = try Regex ( #"(\.xxx)|(\.yyy)|(\.zzz)$"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
90+ _ = try Regex ( #"(^left|right|center)\sbottom$"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
91+ _ = try Regex ( #"\.xxx|\.yyy|\.zzz$"# ) . ignoresCase ( ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
92+ _ = try Regex ( #"\.xxx|\.yyy|\.zzz$"# ) . ignoresCase ( ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
93+ _ = try Regex ( #"\.xxx|\.yyy|zzz$"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
94+ _ = try Regex ( #"^([A-Z]|xxx[XY]$)"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
95+ _ = try Regex ( #"^(xxx yyy zzz)|(xxx yyy)"# ) . ignoresCase ( ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
96+ _ = try Regex ( #"^(xxx yyy zzz)|(xxx yyy)|(1st( xxx)? yyy)|xxx|1st"# ) . ignoresCase ( ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
97+ _ = try Regex ( #"^(xxx:)|(yyy:)|(zzz:)"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
98+ _ = try Regex ( #"^(xxx?:)|(yyy:zzz\/)"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
99+ _ = try Regex ( #"^@media|@page"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
100+ _ = try Regex ( #"^\s*(xxx?|yyy|zzz):|xxx:yyy"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
101+ _ = try Regex ( #"^click|mouse|touch"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
102+ _ = try Regex ( #"^http://good\.com|http://better\.com"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
103+ _ = try Regex ( #"^https?://good\.com|https?://better\.com"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
104+ _ = try Regex ( #"^mouse|touch|click|contextmenu|drop|dragover|dragend"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
105+ _ = try Regex ( #"^xxx:|yyy:"# ) . ignoresCase ( ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
106+ _ = try Regex ( #"_xxx|_yyy|_zzz$"# ) . firstMatch ( in: input) // $ Alert[swift/missing-regexp-anchor] // BAD (missing anchor)
107107 _ = try Regex ( #"em|%$"# ) . firstMatch ( in: input) // BAD (missing anchor) [NOT DETECTED] - not flagged at the moment due to the anchor not being for letters
108108
109109 // the following are MAYBE OK due to apparent complexity; not flagged
0 commit comments