Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

What is the correct format for multi-line if statements as far as golint is concerned? #459

Closed
Zamiell opened this issue Sep 14, 2019 · 4 comments

Comments

@Zamiell
Copy link

Zamiell commented Sep 14, 2019

Greetings. golint complains about the following if statement, claiming that it has a "unnecessary leading newline (whitespace)":

# Multi-line if statement 1
if condition1ThatisReallyLong &&
	condition2ThatisReallyLong &&
	condition3ThatisReallyLong &&
	condition4ThatisReallyLong {

	foo()
}

However, this does not appear to be true - doesn't it have a trailing newline instead of a leading newline? (This appears to be a bug.) Regardless, that isn't even the point of the issue.

golint does not complain about the following if statement:

# Multi-line if statement 2
if condition1ThatisReallyLong &&
	condition2ThatisReallyLong &&
	condition3ThatisReallyLong &&
	condition4ThatisReallyLong {
	foo()
}
  1. Is the second multi-line if statement the preferred form? If yes, isn't this problematic in that it is difficult to distinguish between if statement conditions and block code?
  2. If no, then what is the preferred form of a multi-line if statement in golang?
@ainar-g
Copy link

ainar-g commented Sep 14, 2019

I use the first form, with the leading newline, on all my projects, so I decided to check. I can't reproduce it. What I did:

$ go get golang.org/x/lint/golint@master
go: finding golang.org/x/lint/golint master
go: finding golang.org master
go: finding golang.org/x/lint master
go: finding golang.org/x master
go: downloading golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac
go: extracting golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac
go: downloading golang.org/x/tools v0.0.0-20190311212946-11955173bddd
go: extracting golang.org/x/tools v0.0.0-20190311212946-11955173bddd
go: finding golang.org/x/tools v0.0.0-20190311212946-11955173bddd
$ nl -b a ./main.go
     1	// Package main is obvious.
     2	package main
     3	
     4	func main() {
     5		var x, y, z = 1, 2, 3
     6		if x == 1 &&
     7			y == 2 &&
     8			z == 3 {
     9	
    10			println(1)
    11			return
    12		}
    13	
    14		println(0)
    15	}
$ golint -min_confidence 0.0 ./main.go 
$ echo $?
0

Are you sure the message is actually from golint? Are you using a lint-runner like golangci-lint by any chance?

@Zamiell
Copy link
Author

Zamiell commented Sep 14, 2019

Oh, I am using golangci-lint and it reports that the error is from golint. Is it not actually coming from golint?

@Zamiell
Copy link
Author

Zamiell commented Sep 14, 2019

Oh, my apologies, this seems to be not related to the actual golint linter.

@Zamiell Zamiell closed this as completed Sep 14, 2019
@ainar-g
Copy link

ainar-g commented Sep 14, 2019

I don't know, but a few of my colleagues had the same issues with golangci-lint. It would often report issues and say that it comes from the tool foo, but running foo directly produces no issues. I personally don't use it, as I see no point in it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants