lint/lintutil: only print relative paths if they're shorter
Avoid printing paths like the following:
../../../../../../usr/lib/go/src/fmt/print.go
and instead only print relative paths when they're shorter than the
absolute paths.
This is taken directly from the go tool.
@@ -159,18 +159,19 @@ func ProcessFlagSet(name string, c lint.Checker, fs *flag.FlagSet) {
}
}
-func relativePositionString(pos token.Position) string {
- var s string
- pwd, err := os.Getwd()
- if err == nil {
- rel, err := filepath.Rel(pwd, pos.Filename)
- if err == nil {
- s = rel
+func shortPath(path string) string {
+ cwd, err := os.Getwd()
+ if err != nil {
+ return path
}
+ if rel, err := filepath.Rel(cwd, path); err == nil && len(rel) < len(path) {
+ return rel
}
- if s == "" {
- s = pos.Filename
+ return path
}
+
+func relativePositionString(pos token.Position) string {
+ s := shortPath(pos.Filename)
if pos.IsValid() {
if s != "" {
s += ":"
