Skip to content

Commit a177f34

Browse files
authored
Enable staticcheck QF1006 linter (#242)
Enable the linter which checks for if conditions that could be lifted into loop conditions. This removes nesting and makes the code easier to read (in my opinion).
1 parent 0d6d3a7 commit a177f34

File tree

2 files changed

+59
-94
lines changed

2 files changed

+59
-94
lines changed

.golangci.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,17 @@ linters:
8282
- linters:
8383
- gochecknoglobals
8484
path: validator.go
85+
# Library code uses for loops to implement parsing that often don't have bodies.
86+
# Unfortunately, revive doesn't detect comments within these empty for loops.
87+
- linters:
88+
- revive
89+
text: "empty-block"
90+
path: cel/library.go
8591
### BEGIN Temporary exclusions from golangci-lint upgrade.
8692
# Will remove in a future PR.
8793
- linters:
8894
- staticcheck
8995
text: "QF1001:" # could apply De Morgan's law
90-
- linters:
91-
- staticcheck
92-
text: "QF1006:" # could lift into loop condition
9396
- linters:
9497
- staticcheck
9598
text: "QF1008:" # could remove embedded field "<field>" from selector
@@ -98,6 +101,8 @@ linters:
98101
- third_party$
99102
- builtin$
100103
- examples$
104+
issues:
105+
max-same-issues: 0
101106
formatters:
102107
enable:
103108
- gci

cel/library.go

Lines changed: 51 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,7 @@ func (i *ipv4) addressPrefix() bool {
543543
// prefixLength parses the length of the prefix and stores the value in prefixLen.
544544
func (i *ipv4) prefixLength() bool {
545545
start := i.index
546-
for {
547-
if i.index >= len(i.str) || !i.digit() {
548-
break
549-
}
546+
for i.digit() {
550547
if i.index-start > 2 {
551548
// max prefix-length is 32 bits, so anything more than 2 digits is invalid
552549
return false
@@ -593,10 +590,7 @@ func (i *ipv4) addressPart() bool {
593590
// decOctet parses str from the current index to determine a decimal octet.
594591
func (i *ipv4) decOctet() bool {
595592
start := i.index
596-
for {
597-
if i.index >= len(i.str) || !i.digit() {
598-
break
599-
}
593+
for i.digit() {
600594
if i.index-start > 3 {
601595
// decimal octet can be three characters at most
602596
return false
@@ -626,6 +620,9 @@ func (i *ipv4) decOctet() bool {
626620
//
627621
// DIGIT = %x30-39 ; 0-9
628622
func (i *ipv4) digit() bool {
623+
if i.index >= len(i.str) {
624+
return false
625+
}
629626
c := i.str[i.index]
630627
if '0' <= c && c <= '9' {
631628
i.index++
@@ -678,10 +675,7 @@ func (i *ipv6) getBits() [2]uint64 {
678675
}
679676
// handle double colon, fill pieces with 0
680677
if i.doubleColonSeen {
681-
for {
682-
if len(p16) >= 8 {
683-
break
684-
}
678+
for len(p16) < 8 {
685679
// delete 0 entries at pos, insert a 0
686680
p16 = slices.Insert(p16, i.doubleColonAt, 0x00000000)
687681
}
@@ -735,10 +729,7 @@ func (i *ipv6) addressPrefix() bool {
735729
// prefixLength parses the length of the prefix and stores the value in prefixLen.
736730
func (i *ipv6) prefixLength() bool {
737731
start := i.index
738-
for {
739-
if i.index >= len(i.str) || !i.digit() {
740-
break
741-
}
732+
for i.digit() {
742733
if i.index-start > 3 {
743734
return false
744735
}
@@ -766,10 +757,7 @@ func (i *ipv6) prefixLength() bool {
766757

767758
// addressPart stores the dotted notation for right-most 32 bits in dottedRaw / dottedAddr if found.
768759
func (i *ipv6) addressPart() bool {
769-
for {
770-
if i.index >= len(i.str) {
771-
break
772-
}
760+
for i.index < len(i.str) {
773761
// dotted notation for right-most 32 bits, e.g. 0:0:0:0:0:ffff:192.1.56.10
774762
if (i.doubleColonSeen || len(i.pieces) == 6) && i.dotted() {
775763
dotted := newIpv4(i.dottedRaw)
@@ -842,11 +830,8 @@ func (i *ipv6) zoneID() bool {
842830
func (i *ipv6) dotted() bool {
843831
start := i.index
844832
i.dottedRaw = ""
845-
for {
846-
if i.index < len(i.str) && (i.digit() || i.take('.')) {
847-
continue
848-
}
849-
break
833+
for i.digit() || i.take('.') {
834+
// Consume '*( DIGIT "." )'
850835
}
851836
if i.index-start >= 7 {
852837
i.dottedRaw = i.str[start:i.index]
@@ -866,9 +851,12 @@ func (i *ipv6) dotted() bool {
866851
// If more than 4 hex digits are found, returns an error.
867852
func (i *ipv6) h16() (bool, error) {
868853
start := i.index
869-
for {
870-
if i.index >= len(i.str) || !i.hexdig() {
871-
break
854+
for i.hexdig() {
855+
if i.index-start > 4 {
856+
// too long
857+
// this is an error condition, it means we found a string of more than
858+
// four valid hex digits, which is invalid in ipv6 addresses.
859+
return false, errors.New("invalid hex")
872860
}
873861
}
874862
str := i.str[start:i.index]
@@ -878,12 +866,6 @@ func (i *ipv6) h16() (bool, error) {
878866
// hex digits at the current position.
879867
return false, nil
880868
}
881-
if len(str) > 4 {
882-
// too long
883-
// this is an error condition, it means we found a string of more than
884-
// four valid hex digits, which is invalid in ipv6 addresses.
885-
return false, errors.New("invalid hex")
886-
}
887869

888870
value, err := strconv.ParseUint(str, 16, 16)
889871
if err != nil {
@@ -899,6 +881,9 @@ func (i *ipv6) h16() (bool, error) {
899881
//
900882
// HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
901883
func (i *ipv6) hexdig() bool {
884+
if i.index >= len(i.str) {
885+
return false
886+
}
902887
c := i.str[i.index]
903888
if ('0' <= c && c <= '9') ||
904889
('a' <= c && c <= 'f') ||
@@ -913,6 +898,9 @@ func (i *ipv6) hexdig() bool {
913898
//
914899
// DIGIT = %x30-39 ; 0-9
915900
func (i *ipv6) digit() bool {
901+
if i.index >= len(i.str) {
902+
return false
903+
}
916904
c := i.str[i.index]
917905
if '0' <= c && c <= '9' {
918906
i.index++
@@ -1206,10 +1194,8 @@ func (u *uri) relativePart() bool {
12061194
func (u *uri) scheme() bool {
12071195
start := u.index
12081196
if u.alpha() {
1209-
for {
1210-
if !u.alpha() && !u.digit() && !u.take('+') && !u.take('-') && !u.take('.') {
1211-
break
1212-
}
1197+
for u.alpha() || u.digit() || u.take('+') || u.take('-') || u.take('.') {
1198+
// Consume '*( ALPHA / DIGIT / "+" / "-" / "." )'
12131199
}
12141200
if u.peek(':') {
12151201
return true
@@ -1341,16 +1327,14 @@ func (u *uri) host() bool {
13411327
// Terminated by end of authority.
13421328
func (u *uri) port() bool {
13431329
start := u.index
1344-
for {
1345-
if u.digit() {
1346-
continue
1347-
}
1348-
if u.isAuthorityEnd() {
1349-
return true
1350-
}
1351-
u.index = start
1352-
return false
1330+
for u.digit() {
1331+
// Consume '*DIGIT'
13531332
}
1333+
if u.isAuthorityEnd() {
1334+
return true
1335+
}
1336+
u.index = start
1337+
return false
13541338
}
13551339

13561340
// ipLiteral parses the rule from RFC 6874:
@@ -1381,10 +1365,8 @@ func (u *uri) ipLiteral() bool {
13811365
// Relies on the implementation of isIP.
13821366
func (u *uri) ipv6Address() bool {
13831367
start := u.index
1384-
for {
1385-
if !u.hexdig() && !u.take(':') {
1386-
break
1387-
}
1368+
for u.hexdig() || u.take(':') {
1369+
// Consume '*( HEXDIG / ":" )'
13881370
}
13891371
if isIP(u.str[start:u.index], 6) {
13901372
return true
@@ -1414,10 +1396,8 @@ func (u *uri) ipv6addrz() bool {
14141396
// ZoneID = 1*( unreserved / pct-encoded )
14151397
func (u *uri) zoneID() bool {
14161398
start := u.index
1417-
for {
1418-
if !u.unreserved() && !u.pctEncoded() {
1419-
break
1420-
}
1399+
for u.unreserved() || u.pctEncoded() {
1400+
// Consume '*( unreserved / pct-encoded )'
14211401
}
14221402
if u.index-start > 0 {
14231403
return true
@@ -1431,18 +1411,13 @@ func (u *uri) zoneID() bool {
14311411
// IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
14321412
func (u *uri) ipvFuture() bool {
14331413
start := u.index
1434-
if u.take('v') && u.hexdig() { //nolint:nestif
1435-
for {
1436-
if !u.hexdig() {
1437-
break
1438-
}
1414+
if u.take('v') && u.hexdig() {
1415+
for u.hexdig() {
1416+
// Consume '*HEXDIG'
14391417
}
14401418
if u.take('.') {
14411419
counter := 0
1442-
for {
1443-
if !u.unreserved() && !u.subDelims() && !u.take(':') {
1444-
break
1445-
}
1420+
for u.unreserved() || u.subDelims() || u.take(':') {
14461421
counter++
14471422
}
14481423
if counter >= 1 {
@@ -1492,10 +1467,8 @@ func (u *uri) isPathEnd() bool {
14921467
// Terminated by end of path: "?", "#", or end of URI.
14931468
func (u *uri) pathAbempty() bool {
14941469
start := u.index
1495-
for {
1496-
if !u.take('/') || !u.segment() {
1497-
break
1498-
}
1470+
for u.take('/') && u.segment() {
1471+
// Consume '*( "/" segment )'
14991472
}
15001473
if u.isPathEnd() {
15011474
return true
@@ -1513,10 +1486,8 @@ func (u *uri) pathAbsolute() bool {
15131486
start := u.index
15141487
if u.take('/') {
15151488
if u.segmentNz() {
1516-
for {
1517-
if !u.take('/') || !u.segment() {
1518-
break
1519-
}
1489+
for u.take('/') && u.segment() {
1490+
// Consume '*( "/" segment )'
15201491
}
15211492
}
15221493
if u.isPathEnd() {
@@ -1535,10 +1506,8 @@ func (u *uri) pathAbsolute() bool {
15351506
func (u *uri) pathNoscheme() bool {
15361507
start := u.index
15371508
if u.segmentNzNc() {
1538-
for {
1539-
if !u.take('/') || !u.segment() {
1540-
break
1541-
}
1509+
for u.take('/') && u.segment() {
1510+
// Consume *( "/" segment )
15421511
}
15431512
if u.isPathEnd() {
15441513
return true
@@ -1556,10 +1525,8 @@ func (u *uri) pathNoscheme() bool {
15561525
func (u *uri) pathRootless() bool {
15571526
start := u.index
15581527
if u.segmentNz() {
1559-
for {
1560-
if !u.take('/') || !u.segment() {
1561-
break
1562-
}
1528+
for u.take('/') && u.segment() {
1529+
// Consume *( '/' segment )
15631530
}
15641531
if u.isPathEnd() {
15651532
return true
@@ -1582,10 +1549,8 @@ func (u *uri) pathEmpty() bool {
15821549
//
15831550
// segment = *pchar
15841551
func (u *uri) segment() bool {
1585-
for {
1586-
if !u.pchar() {
1587-
break
1588-
}
1552+
for u.pchar() {
1553+
// Consume '*pchar'
15891554
}
15901555
return true
15911556
}
@@ -1608,13 +1573,8 @@ func (u *uri) segmentNz() bool {
16081573
// ; non-zero-length segment without any colon ":"
16091574
func (u *uri) segmentNzNc() bool {
16101575
start := u.index
1611-
for {
1612-
if !u.unreserved() &&
1613-
!u.pctEncoded() &&
1614-
!u.subDelims() &&
1615-
!u.take('@') {
1616-
break
1617-
}
1576+
for u.unreserved() || u.pctEncoded() || u.subDelims() || u.take('@') {
1577+
// Consume '*( unreserved / pct-encoded / sub-delims / "@" )'
16181578
}
16191579
if u.index-start > 0 {
16201580
return true

0 commit comments

Comments
 (0)