Skip to content

fix: remove redundant nil check#3089

Merged
dnwe merged 1 commit intoIBM:mainfrom
hrtsegv:fix/remove-unnecessary-nil-check
Feb 3, 2025
Merged

fix: remove redundant nil check#3089
dnwe merged 1 commit intoIBM:mainfrom
hrtsegv:fix/remove-unnecessary-nil-check

Conversation

@hrtsegv
Copy link
Copy Markdown
Contributor

@hrtsegv hrtsegv commented Jan 31, 2025

mocks/async_producer.go remove unnecessay nil check.

var slice []byte
if slice == nil || len(slice) == 0 {}
// is the same as
if len(slice) == 0 {}

because len(slice) where slice == nil is always equal to 0,

staticcheck will highlight this optimization with the following message:

mocks/async_producer.go:77:7: should omit nil check; len() for []*github.com/IBM/sarama/mocks.producerExpectation is defined as zero (S1009)

for reference: staticcheck S1009

Copy link
Copy Markdown
Collaborator

@puellanivis puellanivis left a comment

Choose a reason for hiding this comment

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

Yep. 👍

@dnwe
Copy link
Copy Markdown
Collaborator

dnwe commented Feb 1, 2025

Interesting that we’re not getting this flagged by staticcheck in our golangci-lint runs as we definitely have sc enabled here

@puellanivis
Copy link
Copy Markdown
Collaborator

Interesting that we’re not getting this flagged by staticcheck in our golangci-lint runs as we definitely have sc enabled here

disable-all: true

  disable-all: true

This might be related?

@dnwe
Copy link
Copy Markdown
Collaborator

dnwe commented Feb 2, 2025

  disable-all: true

This might be related?

That’s just the config equivalent of the cli flag to disable all the default linters and only enable explicitly defined ones

@dnwe
Copy link
Copy Markdown
Collaborator

dnwe commented Feb 2, 2025

Very strange, run via golangci-lint we get no results for staticcheck even if we disable all config and default excludes:

% golangci-lint-1.62.2 run --no-config --disable-all --enable=staticcheck --exclude-use-default=false .

But standalone staticcheck flags a few different things including S1009:

% staticcheck ./...
balance_strategy.go:993:30: func (*partitionMovements).isLinked is unused (U1000)
balance_strategy.go:1028:30: func (*partitionMovements).in is unused (U1000)
balance_strategy.go:1043:30: func (*partitionMovements).hasCycles is unused (U1000)
balance_strategy.go:1075:30: func (*partitionMovements).isSticky is unused (U1000)
balance_strategy.go:1093:6: func indexOfSubList is unused (U1000)
metrics_helpers_test.go:120:6: func minMaxHistogramValidator is unused (U1000)
mocks/async_producer.go:77:7: should omit nil check; len() for []*github.com/IBM/sarama/mocks.producerExpectation is defined as zero (S1009)
mocks/mocks.go:55:41: error strings should not be capitalized (ST1005)
mocks/mocks.go:56:41: error strings should not be capitalized (ST1005)

@hrtsegv
Copy link
Copy Markdown
Contributor Author

hrtsegv commented Feb 2, 2025

the staticcheck linter used by golangci-lint isn't the same as the standalone staticcheck tool.
according to golangci-lint's documentation, the author of staticcheck does not approve its use as a library inside golangci-lint.
this could explain why some checks, like S1009, are flagged when running staticcheck directly but not via golangci-lint.

golangci-lint-docs-staticcheck

for reference: https://golangci-lint.run/usage/linters/#staticcheck

Copy link
Copy Markdown
Collaborator

@dnwe dnwe left a comment

Choose a reason for hiding this comment

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

Lgtm

@dnwe dnwe merged commit 685ed7e into IBM:main Feb 3, 2025
@dnwe dnwe added the fix label Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants