Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: differentiate no result and match #170

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions .grit/patterns/css/aspect_ratio.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,35 @@ a {
some-property: 5px;
}

a.b ~ c.d {}
.e.f + .g.h {}
a.b ~ c.d {
}
.e.f + .g.h {
}

@font-face {
font-family: 'Open Sans';
src: url('/a') format('woff2'), url('/b/c') format('woff');
}
```

```css
a {
width: calc(100% - 80px);
aspect-ratio: 1/2;
font-size: calc(10px + (56 - 10) * ((100vw - 320px) / (1920 - 320)));
}

#some-id {
some-property: 5px;
}

a.b ~ c.d {
}
.e.f + .g.h {
}

@font-face {
font-family: "Open Sans";
src: url("/a") format("woff2"), url("/b/c") format("woff");
font-family: 'Open Sans';
src: url('/a') format('woff2'), url('/b/c') format('woff');
}
```
40 changes: 0 additions & 40 deletions .grit/patterns/go/channel_guarded_with_mutex.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Detected a channel guarded with a `mutex`. Channels already have an internal `mu

- [go-antipatterns](https://hackmysql.com/golang/go-antipatterns/#guarded-channel)


```grit
language go

Expand Down Expand Up @@ -138,42 +137,3 @@ func main() {
wg.Wait()
}
```

```go
package main

import (
"fmt"
"sync"
)

func main() {
var wg sync.WaitGroup
var mu sync.Mutex
data := make([]int, 0)

// Producer
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 5; i++ {
mu.Lock()
data = append(data, i)
mu.Unlock()
}
}()

// Consumer
wg.Add(1)
go func() {
defer wg.Done()
mu.Lock()
for _, num := range data {
fmt.Println(num)
}
mu.Unlock()
}()

wg.Wait()
}
```
29 changes: 2 additions & 27 deletions .grit/patterns/go/exported_loop_pointer.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func() {

```

## loop iterations with with pointers and values assigned to new varible
## loop iterations with pointers and values assigned to new variable

```go
func() {
Expand All @@ -87,32 +87,7 @@ func() {

```

```go
func() {
values := []string{"a", "b", "c"}
var funcs []func()
for _, val := range values {
val := val // pin!
funcs = append(funcs, func() {
fmt.Println(&val)
})
}
}

```

## loop iterations with without pointers

```go
func (){
input := []string{"a", "b", "c"}
output := []string{}
for _, val := range input {
output = append(output, val)
}
}

```
## loop iterations without pointers

```go
func (){
Expand Down
50 changes: 11 additions & 39 deletions .grit/patterns/go/hidden_goroutine.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
---
title: Detected a hidden goroutine
title: Detect a hidden goroutine
tags: [correctness, best-practice]
---

Function invocations are expected to synchronous, and this function will execute asynchronously because all it does is call a goroutine. Instead, remove the internal goroutine and call the function using `go`.


```grit
language go

file($name, $body) where {
$body <: contains `func $func() { go func() { $funcBody }() }` as $hiddenFunc => `func $func() {
$funcBody
$body <: contains `func $func() { go func() { $funcBody }() }` as $hiddenFunc => `func $func() {
$funcBody
}`,
$body <: maybe contains `func main(){ $mainBody }` where {
$mainBody <: contains `$func()` => `go $func()`
}
}
```

## Detected a hidden goroutine with func call
## Detect a hidden goroutine with func call

```go
package main
Expand All @@ -45,8 +44,8 @@ package main
import "fmt"

// hidden goroutine
func HiddenGoroutine() {
fmt.Println("hello world")
func HiddenGoroutine() {
fmt.Println("hello world")
}

func main() {
Expand All @@ -55,7 +54,7 @@ func main() {
}
```

## Detected a hidden goroutine without func call
## Detect a hidden goroutine without func call

```go
package main
Expand All @@ -76,26 +75,12 @@ package main
import "fmt"

// hidden goroutine
func HiddenGoroutine() {
fmt.Println("hello world")
func HiddenGoroutine() {
fmt.Println("hello world")
}
```

## Detected a hidden goroutine with other operation on top

```go
// hidden goroutine
func FunctionThatCallsGoroutineIsOk() {
fmt.Println("This is normal")
go func() {
fmt.Println("This is OK because the function does other things")
}()
}

func main() {
FunctionThatCallsGoroutineIsOk()
}
```
## Detect a hidden goroutine with other operation on top

```go
// hidden goroutine
Expand All @@ -111,20 +96,7 @@ func main() {
}
```

## Detected a hidden goroutine with other operation on bottom
```go
// hidden goroutine
func FunctionThatCallsGoroutineAlsoOk() {
go func() {
fmt.Println("This is OK because the function does other things")
}()
fmt.Println("This is normal")
}

func main() {
FunctionThatCallsGoroutineAlsoOk()
}
```
## Detect a hidden goroutine with other operation on bottom

```go
// hidden goroutine
Expand Down
37 changes: 9 additions & 28 deletions .grit/patterns/go/useless_if_else_body.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ tags: [fix, correctness]

Identical statements found in both the `if` and `else` bodies of an `if-statement`. This results in the same code execution regardless of the if-expression outcome. To optimize, eliminate the `if` statement entirely.


```grit
language go

or {
`if ($conditon) { $body } else { $body }`,
`if ($conditon) { $body } else if ($conditon) { $body }`
} => `if ($conditon) {
$body
} => `if ($conditon) {
$body
}`
```

Expand All @@ -37,7 +36,7 @@ func main() {
} else if (y) {
fmt.Println("same condition")
}

}
```

Expand All @@ -54,10 +53,10 @@ func main() {
}

// useless-if-conditional
if (y) {
fmt.Println("same condition")
if (y) {
fmt.Println("same condition")
}

}
```

Expand All @@ -80,7 +79,6 @@ func main() {
}
```


```go
package main

Expand All @@ -90,8 +88,8 @@ func main() {
var y = 1

// useless-if-body
if (y) {
fmt.Println("of course")
if (y) {
fmt.Println("of course")
}
}
```
Expand All @@ -105,28 +103,11 @@ import "fmt"

func main() {
var y = 1

if (y) {
fmt.Println("of course")
} else {
fmt.Println("different condition")
}
}
```


```go
package main

import "fmt"

func main() {
var y = 1

if (y) {
fmt.Println("of course")
} else {
fmt.Println("different condition")
}
}
```
```
12 changes: 0 additions & 12 deletions .grit/patterns/java/unnecessary_equality_comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,3 @@ class Bar {
}
}
```

```java
class Bar {
void main() {
boolean myBoolean;
boolean myBoolean2;
if(myBoolean == myBoolea2){
continue;
}
}
}
```
11 changes: 9 additions & 2 deletions .grit/patterns/js/_no_restricted_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This pattern provides the equivalent of the `no-restricted-imports` [rule in ESLint](https://eslint.org/docs/latest/rules/no-restricted-imports).

You *must* provide a pattern that will match the imports you want to restrict.
You _must_ provide a pattern that will match the imports you want to restrict.

```grit
language js
Expand All @@ -11,19 +11,26 @@ language js
no_restricted_imports($modules=or {"lodash", r"@shared/internal/(.*)"($_)})
```


## Lodash example

```js
import { get } from 'lodash';
```

```js
import { get } from 'lodash';
```

## Internal example

```js
import { get } from '@shared/internal/whatever';
```

```js
import { get } from '@shared/internal/whatever';
```

## Clean example

```js
Expand Down
Loading
Loading