Skip to content

durationcheck: False positive when multiplying with int type struct field #1744

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

Merged
merged 1 commit into from
Feb 20, 2021
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/ashanbrown/makezero v0.0.0-20201205152432-7b7cdbb3025a
github.com/bkielbasa/cyclop v1.2.0
github.com/bombsimon/wsl/v3 v3.1.0
github.com/charithe/durationcheck v0.0.3
github.com/charithe/durationcheck v0.0.4
github.com/daixiang0/gci v0.2.8
github.com/denis-tingajkin/go-header v0.4.2
github.com/esimonov/ifshort v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions test/testdata/durationcheck.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
//args: -Edurationcheck
package testdata

import "time"
import (
"fmt"
"time"
)

func waitFor(someDuration time.Duration) {
timeToWait := someDuration * time.Second // ERROR "Multiplication of durations: `someDuration \\* time.Second` "
type durationCheckData struct {
i int
d time.Duration
}

func durationcheckCase01() {
dcd := durationCheckData{i: 10}
_ = time.Duration(dcd.i) * time.Second
}

func durationcheckCase02() {
dcd := durationCheckData{d: 10 * time.Second}
_ = dcd.d * time.Second // ERROR "Multiplication of durations: `dcd.d \\* time.Second`"
}

func durationcheckCase03() {
seconds := 10
fmt.Print(time.Duration(seconds) * time.Second)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't this yield an error? 🤔

Copy link
Member Author

@ldez ldez Feb 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because time.Duration is a int64 (type Duration int64) so in this case: seconds (int) is just converted to int64 (Duration).

time.Duration(10) means 10 nanoseconds.

time.Duration(seconds) * time.Second
=> int64(seconds) * time.Second
=> 10 * time.Second

}

func durationcheckCase04(someDuration time.Duration) {
timeToWait := someDuration * time.Second // ERROR "Multiplication of durations: `someDuration \\* time.Second`"
time.Sleep(timeToWait)
}

func durationcheckCase05() {
someDuration := 2 * time.Second
timeToWait := someDuration * time.Second // ERROR "Multiplication of durations: `someDuration \\* time.Second`"
time.Sleep(timeToWait)
}