-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 lint errors on files with //line directive #1065
Fix lint errors on files with //line directive #1065
Conversation
If the target files contains `//line` directive and it indicates a non-go file, the linter is going to handle it as a go file, which results in failure. The cause of this issue is that the linters (`Analyzer`s) are using `pass.Fset.Position()`. This func returns the adjusted position using `//line` directive. The example project reported in golangci#998 has `//line` directive that indicates other non-go file. According to the description of "Compiler Directives” (https://golang.org/cmd/compile/#hdr-Compiler_Directives), line directives is mainly used for reporting original positions to the generators or something. On linters of golangci-lint, `pass.Fset.Position()` is used just to aggregate file names; we don't have to adjust positions. This changes `Analyzer`s that use `pass.Fset.Position()` to aggregate file names to use `pass.Fset.PositionFor()` with `adjusted == false`. Relates: golangci#998
hi, thank you for the pull request! Please, add some tests for it. For example, when I've done a similar fix I've added these tests. |
Thank you for you review. I see, I'll do it. |
I've added tests for the fix. |
thank you, I'll look at it a bit later, |
thank you! |
This change fixes a bug caused that many linters ignored all files that are using Cgo. It was introduced by PR golangci#1065, which assumed that all files referenced by //line directives are non-Go files, which is not true. In the case of Cgo they point to the original Go files which are used by Cgo as templates to generate other Go files. The fix replaces all calls of Pass.Fset.PositionFor with a function `getFileNames()` that first calls Pass.Fset.PositionFor with adjustment, and only if it results in a non-Go file it calls Pass.Fset.PositionFor again without adjustment. Fixes: golangci#2910 Signed-off-by: Sven Anderson <sven@anderson.de>
This change fixes a bug caused that many linters ignored all files that are using Cgo. It was introduced by PR golangci#1065, which assumed that all files referenced by //line directives are non-Go files, which is not true. In the case of Cgo they point to the original Go files which are used by Cgo as templates to generate other Go files. The fix replaces all calls of Pass.Fset.PositionFor with a function getFileNames() that first calls Pass.Fset.PositionFor with adjustment, and only if it results in a non-Go file it calls Pass.Fset.PositionFor again without adjustment. Fixes: golangci#2910 Signed-off-by: Sven Anderson <sven@anderson.de>
This change fixes a bug caused that many linters ignored all files that are using Cgo. It was introduced by PR golangci#1065, which assumed that all files referenced by //line directives are non-Go files, which is not true. In the case of Cgo they point to the original Go files which are used by Cgo as templates to generate other Go files. The fix replaces all calls of Pass.Fset.PositionFor with a function getFileNames() that first calls Pass.Fset.PositionFor with adjustment, and only if it results in a non-Go file it calls Pass.Fset.PositionFor again without adjustment. Fixes: golangci#2910 Signed-off-by: Sven Anderson <sven@anderson.de>
This will fix #998.
If the target files contains
//line
directive and it indicates a non-go file, the linter is going to handle it as a go file, which results in failure.The cause of this issue is that the linters (
Analyzer
s) are usingpass.Fset.Position()
.This func returns the adjusted position using
//line
directive.The example project reported in #998 has
//line
directive that indicates other non-go file.According to the description of Compiler Directives, line directives is mainly used for reporting original positions to the generators or something.
On linters of golangci-lint,
pass.Fset.Position()
is used just to aggregate file names; we don't have to adjust positions.This pull request changes
Analyzer
s that usepass.Fset.Position()
to aggregate file names to usepass.Fset.PositionFor()
withadjusted == false
.