-
Notifications
You must be signed in to change notification settings - Fork 285
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
fix redunant-import-alias rule - unused alias is also redundant #950
Closed
mfederowicz
wants to merge
5
commits into
mgechev:master
from
mfederowicz:fix-redundant-import-alias
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03826dd
add param ignoreUsed to ignore redundant aliases but used in code (us…
mfederowicz bb05fd3
cleanup docs
mfederowicz 918f65a
cleanup testdata from wrong cases
mfederowicz 0ef6c87
refactor: invert conditions to reduce indent, restore old testdata, a…
mfederowicz 45cfdc6
refactor function format
mfederowicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,33 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/mgechev/revive/rule" | ||
"testing" | ||
|
||
"github.com/mgechev/revive/lint" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
// TestRedundantImportAlias rule. | ||
func TestRedundantImportIgnoredAliases(t *testing.T) { | ||
|
||
args := []any{map[string]any{ | ||
"ignoreUsed": true, | ||
}} | ||
|
||
testRule(t, "redundant-import-alias-ignored", &rule.RedundantImportAlias{}, &lint.RuleConfig{ | ||
Arguments: args, | ||
}) | ||
|
||
} | ||
|
||
func TestRedundantImportAlias(t *testing.T) { | ||
testRule(t, "redundant-import-alias", &rule.RedundantImportAlias{}) | ||
|
||
args := []any{map[string]any{ | ||
"ignoreUsed": false, | ||
}} | ||
|
||
testRule(t, "redundant-import-alias", &rule.RedundantImportAlias{}, &lint.RuleConfig{ | ||
Arguments: args, | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package fixtures | ||
|
||
import ( | ||
runpb "cloud.google.com/go/run/apiv2/runpb" | ||
"crypto/md5" | ||
md5 "crypto/md5" | ||
"strings" | ||
str "strings" // MATCH /Import alias "str" is redundant/ | ||
strings "strings" // MATCH /Import alias "strings" is redundant/ | ||
) | ||
|
||
func UseRunpb() { | ||
runpb.RegisterTasksServer() | ||
} | ||
|
||
func UseMd5() { | ||
fmt.PrintLn(md5.Size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
package fixtures | ||
|
||
import( | ||
import ( | ||
"crypto/md5" | ||
"strings" | ||
_ "crypto/md5" | ||
mfederowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
str "strings" | ||
strings "strings" // MATCH /Import alias "strings" is redundant/ | ||
crypto "crypto/md5" | ||
md5 "crypto/md5" // MATCH /Import alias "md5" is redundant/ | ||
md5 "crypto/md5" // MATCH /Import alias "md5" is redundant/ | ||
|
||
runpb "cloud.google.com/go/run/apiv2/runpb" // MATCH /Import alias "runpb" is redundant/ | ||
) | ||
|
||
func UseRunpb() { | ||
runpb.RegisterTasksServer() | ||
} | ||
|
||
func UseMd5() { | ||
fmt.PrintLn(md5.Size) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please improve the wording? It's not really clear for me what exactly is to be ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import apiv2 "cloud.google.com/go/run/apiv2"
import v1 "https://pkg.go.dev/k8s.io/api/core/v1"
technically above aliases are correct if they are used min one time later in code. What is your sugestion to describe that flag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here is that if it's not used, the code won't compile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no no no, your case is not that case, because this code is not used and make "noise" after compile
but in case mentioned in #936:
above code is working well, but linter shows warning:
after set ignoreUsed = true
linter dont produce warnings that is the case :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is indeed redundant. This is the purpose of the linter. If you think it's fine for your project, you should just disable this linter rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes i know, it is specific case, because in one way you should think how you use packages and aliases for them. On the other side we can leave this rule with that argument for more flexibility, but this is not my decision, what you think @chavacava
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is specific about this case?
ignoreUsed
seems to be essentially disabling the rule.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denisvmedia ok, I think that issue #936 and this pull request is to close, because in standard scenario this rule is used to detect redundant aliases, and results should used to fix/cleanup codebase of your project (change redundant aliases to proper form)