uartois / sonar-golang

Sonarqube plugin for the golang language.
GNU Lesser General Public License v3.0
245 stars 32 forks source link

Comment lines are computed as executable in not-tested source file #61

Closed lylex closed 6 years ago

lylex commented 6 years ago

Description

When a package has no test file, it will computed the comment lines as executable.

Steps to Reproduce

  1. Create a project with following source file(e.g. b.go)
package main

import (
        "fmt"
)

// OtherFunc comments goes here.
func OtherFunc() {
        fmt.Println("in OtherFunc")
}

// Foo comments goes here.
// Another line of comments.
func Foo() {
        // test comments
        fmt.Println("in foo")
}

// MyStruct is a struct.
type MyStruct struct {
        // A comments.
        A string

        // B comments.
        B string
}
  1. Run the calculation, and push to sonar server using sonar-scanner

  2. Go the sonar server web, and click "All Measures / Coverage Measures". Chose b.go file, and you you will find that some comments are calculated as executable lines.

Versions

Sonar Server: 6.5(build 27846) Sonar-golang Plugin: 1.2.11-rc11 GO: go1.9.2

danielleberre commented 6 years ago

Ok, we forgot to ignore comments in the fix for #60 .

We can fix this easily by ignoring lines starting with "//" once trimmed.

However, ignoring multi-lines comments will be much harder until we have an analysis of the source file based on a real go grammar.

@thibaultfalque the fix for single line comments is trivial. For multi-lines, it may be possible to reuse the same trick used to ignore import statements, but based on the detection of the beginning and end of the multi-line comments.

thibaultfalque commented 6 years ago

Ok I see. I will fix this problem.

thibaultfalque commented 6 years ago

I have fix this issue.

The fix: d3889bf The units tests: ee8c96fb4586509b4ce6b901ff8e6153494beb9d
The units test are apply on this file: issue61/simplelinecomment.go issue61/multilinecoment.go issue61/typestruct.go: 669e4e184f120f4451fe9650317b207880c2024a

thibaultfalque commented 6 years ago

@lylex could you test the last release ?

lylex commented 6 years ago

@thibaultfalque @danielleberre this release work well with my project, thanks for the quick response.