Skip to content

Conversation

@navijation
Copy link

This PR introduces a new forbid-improper-conversions flag which, when enabled, will cause the analyzer to raise
errors on invocations of time.Duration() on non-duration integer types unless these invocations are multiplied
by "proper" durations like time.Millisecond subsequently.

This helps capture a class of bugs caused by a lack of awareness that time.Duration() converts its integer arguments
to nanoseconds. It would be nice to forbid conversions via time.Duration() entirely, but it is necessary
because existing integer variables such as "timeout in milliseconds" cannot be multiplied by the time units due to
having a defined and mismatching type compared to time.Duration.

Resolves #1

// `var Name = 123`, and `a := 123` declarations are already type-checked
// by the compiler
func hasUntypedConstDeclaration(ti *types.Info, identifier *ast.Ident) bool {
func hasIntOrUntypedConstDeclaration(ti *types.Info, identifier *ast.Ident) bool {
Copy link
Author

Choose a reason for hiding this comment

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

This is necessary to handle Sleep(time.Duration(someExistingIntegerInSecondsConstant)) correctly, but it needs to be rethought to handle a greater variety of cases.


func (s *improperDurationContextStack) Finish() {
for _, diagnostic := range s.sentinel.deferredImproperDurationDiagnostics {
s.ReportImproperDurationNode(diagnostic)
Copy link
Author

Choose a reason for hiding this comment

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

This should be s.pass.Report(diagnostic). Although sentinel in practice will never have diagnostics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow runners to forbid time.Duration casting on expressions of integer type

1 participant