Skip to content

Commit

Permalink
Improve rule accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
risto-liftoff committed Jul 19, 2023
1 parent 40967dd commit 1be8bb3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
36 changes: 26 additions & 10 deletions go/invalid-usage-of-modified-variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func main() {
}

// ruleid: invalid-usage-of-modified-variable
eng11, err := getEngineerAtIndex(engineers, 5)
if err != nil {
eng11, err2 := getEngineerAtIndex(engineers, 5)
if err2 != nil {
fmt.Printf("Something")
fmt.Printf("Unable to obtain engineer with FName %s\n", eng11.FName)
fmt.Printf("Something")
Expand All @@ -60,6 +60,14 @@ func main() {
fmt.Printf("Something")
}

// ruleid: invalid-usage-of-modified-variable
eng14, addr1, err := getEngineerAndAddressByIndex(engineers, 5)
if err != nil {
fmt.Printf("Something")
fmt.Printf("Unable to obtain engineer with FName %s\n", eng14.FName)
fmt.Printf("Something")
}

// BASIC TESTS - inline if
// ruleid: invalid-usage-of-modified-variable
if eng2, err := getEngineerAtIndex(engineers, 6); err != nil {
Expand Down Expand Up @@ -185,19 +193,17 @@ func main() {
eng91 = &Engineer{0, "N/A", "N/A", 0, nil}
}

// TRUE POSITIVES

var eng103 *Engineer
// ruleid: invalid-usage-of-modified-variable
eng103, err = getEngineerAtIndex(engineers, 10)
// ok: invalid-usage-of-modified-variable
eng92, addr2, err := getEngineerAndAddressByIndex(engineers, 5)
if err != nil {
fmt.Printf("Something")
fmt.Printf("Unable to obtain engineer %d!\n", eng103.Id)
eng103 := &Engineer{0, "N/A", "N/A", 0, nil}
fmt.Printf("Unable to obtain engineer %d!\n", eng103.Id)
eng92, err = getEngineerAtIndex(engineers, 6)
fmt.Printf("Something")
}


// TRUE POSITIVES

// ruleid: invalid-usage-of-modified-variable
eng11, err = getEngineerAtIndex(engineers, 8)
if err != nil {
Expand Down Expand Up @@ -247,6 +253,9 @@ func main() {
fmt.Printf("Engineer 71: %s", fmt.Sprintf("%s %s", eng7.FName, eng71.LName))
fmt.Printf("Engineer 9: %s", fmt.Sprintf("%s %s", eng9.FName, eng9.LName))
fmt.Printf("Engineer 91: %s", fmt.Sprintf("%s %s", eng91.FName, eng91.LName))
fmt.Printf("Engineer 92: %s", fmt.Sprintf("%s %s", eng92.FName, eng92.LName))
fmt.Printf("Address 1: %v", addr1)
fmt.Printf("Address 2: %v", addr2)
}

func getEngineerAtIndex(slice []Engineer, idx int) (*Engineer, error) {
Expand All @@ -263,6 +272,13 @@ func getEngineerAddressByIndex(slice []Engineer, idx int) (*Address, error) {
return slice[idx].Address, nil
}

func getEngineerAndAddressByIndex(slice []Engineer, idx int) (*Engineer, *Address, error) {
if idx >= len(slice) {
return nil, nil, fmt.Errorf("invalid index")
}
return &slice[idx], slice[idx].Address, nil
}

func getEngineers() []Engineer {
return []Engineer{
{
Expand Down
17 changes: 9 additions & 8 deletions go/invalid-usage-of-modified-variable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@ rules:

patterns:
- pattern: |
$X, err = ...
if err != nil {
..., $X, ..., $ERR = ...
if $ERR != nil {
...
<... $X ...>
}
- pattern-not: |
$X, err = ...
if err != nil {
..., $X, ..., $ERR = ...
if $ERR != nil {
...
$X = ...
$X, ... = ...
...
<... $X ...>
}
- pattern-not: |
$X, err = ...
if err != nil {
..., $X, ..., $ERR = ...
if $ERR != nil {
...
$X = ...
...
}

0 comments on commit 1be8bb3

Please sign in to comment.