7

Here is my license header in the source code:

package org.osgl.ut;

/*-
 * #%L
 * Java Unit Test Tool
 * %%
 * Copyright (C) 2017 OSGL (Open Source General Library)
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import static org.hamcrest.Matchers.not;

import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.internal.ArrayComparisonFailure;
import org.junit.internal.ExactComparisonCriteria;
import org.junit.internal.InexactComparisonCriteria;

/**
 * The `TestBase` provides simplified assertion methods.
 */
public abstract class TestBase extends Assert {

    /**
     * Asserts that a condition is `true`. If it isn't then throws an
     * {@link AssertionError} with the given message.
     *
     * @param condition
     *              condition to be checked
     * @param message
     *              The error message. `null` Okay
     * @param messageArgs
     *              the error message arguments
     */
    public static void yes(boolean condition, String message, Object ... messageArgs) {
        assertTrue(fmt(message, messageArgs), condition);
    }

    /**
     * Alias of {@link #assertTrue(boolean)}.
     *
     * @param condition condition to be checked
     */
    public static void yes(boolean condition) {
        assertTrue(condition);
    }

    /**
     * Alias of {@link #assertThat(Object, Matcher)}.
     *
     * @param actual
     *              the computed value being compared
     * @param matcher
     *              an expression, built of {@link Matcher}s, specifying allowed values
     * @param <T>
     *              the static type accepted by the matcher (this can flag obvious
     *              compile-time problems such as `yes(1, is("a"))`
     *
     * @see org.hamcrest.CoreMatchers
     * @see org.junit.matchers.JUnitMatchers
     */
    public static <T> void yes(T actual, Matcher<T> matcher) {
        assertThat(actual, matcher);
    }

    /**
     * Require `actual` satisfied the condition specified by `matcher`. If not
     * an {@link AssertionError} is thrown with the reason string and information
     * about the matcher and failing value. Example:
     *
     * ```
     * int n = 0;
     * yes(n, is(not(1))) // passes
     * yes(n, is(1), "Help! Integers don't work"); // fails:
     * // failure message:
     * // Help! Integers don't work
     * // expected: is <1>
     * // got value: <0>
     * ```
     * @param actual the computed value being compared
     * @param matcher an expression, built of {@link Matcher}s, specifying allowed values
     * @param message
     *              additional information about the error
     * @param messageArgs
     *              message arguments
     * @param <T>
     *              the static type accepted by the matcher (this can flag obvious
     *              compile-time problems such as `yes(1, is("a"))`
     *
     * @see org.hamcrest.CoreMatchers
     * @see org.junit.matchers.JUnitMatchers
     */
    public static <T> void yes(T actual, Matcher<T> matcher, String message, Object... messageArgs) {
        if (!matcher.matches(actual)) {
            assertThat(fmt(message, messageArgs), actual, matcher);
        }
    }

    /**
     * Alias of {@link #assertFalse(boolean)}.
     *
     * @param condition condition to be checked
     */
    public static void no(boolean condition) {
        assertFalse(condition);
    }

    /**
     * Asserts that a condition is `false`. If it isn't then throws an
     * {@link AssertionError} with the given message.
     *
     * @param condition
     *              condition to be checked
     * @param message
     *              The error message. `null` Okay
     * @param messageArgs
     *              the error message arguments
     */
    public static void no(boolean condition, String message, Object... messageArgs) {
        assertTrue(String.format(message, messageArgs), !condition);
    }

    /**
     * Require `actual` **NOT** satisfied the condition specified by `matcher`. Otherwise
     * an {@link AssertionError} is thrown with the reason string and information
     * about the matcher and failing value. Example:
     *
     * ```
     * int n = 0;
     * no(n, is(1)) // passes
     * no(n, is(0)); // fails:
     * // failure message:
     * // expected: not is <0>
     * // got value: <0>
     * ```
     *
     * @param actual
     *              the computed value being compared
     * @param matcher
     *              an expression, built of {@link Matcher}s, specifying disallowed values
     * @param <T>
     *              the static type accepted by the matcher (this can flag obvious
     *              compile-time problems such as `no(1, is("a"))`
     *
     * @see org.hamcrest.CoreMatchers
     * @see org.junit.matchers.JUnitMatchers
     */
    public static <T> void no(T actual, Matcher<T> matcher) {
        assertThat(actual, not(matcher));
    }
    ...

And when I run SonarLint it says there is an issue found in the file:

enter image description here

How can I get SonarLint skip License header block when inspecting the source code ?

Update

The SonarLint version:

SonarLint version: 3.0

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
  • For some reason it seems to think the comments contain code. Please [edit] your post and copy/paste the first 30-40 lines of the file (not just the license header) into your post in addition to the screenshot. – Jim Garrison Aug 25 '17 at 04:57
  • Have you checked if there's an outstanding bug in SonarLint? – Jim Garrison Aug 25 '17 at 04:58
  • Haven't checked if it is reported bug. Update the question and added link to the source code file – Gelin Luo Aug 25 '17 at 05:24
  • We discourage links to offsite resources as the question is expected to stand on its own over time. The point is to be a resource for other people in the future and if your link disappears the question becomes useless. – Jim Garrison Aug 25 '17 at 05:29
  • pasted the first 162 lines of the file including license header. Removed offsite resource link – Gelin Luo Aug 25 '17 at 06:55
  • Which version of SonarLint are you using? Can you try to upgrade to latest version? – Tibor Blenessy Aug 25 '17 at 08:35
  • My SonarLint comes with the IntelliJ IDEA plugin: 3.0.0.2.041. – Gelin Luo Aug 25 '17 at 10:08
  • @green i just removed `;` from `Licensed under the Apache License, Version 2.0 (the "License");` and sonar lint warning gone. – Snehal Patel Aug 25 '17 at 10:19
  • 1
    If I understand correctly, you have your license below the `package ` statement? Can you put your license on top of your file? This should remove the issue. – Tibor Blenessy Aug 25 '17 at 12:08
  • The license was added by license-maven-plugin from org.codehaus.mojo – Gelin Luo Aug 26 '17 at 11:36
  • Here comes a follow-up issue. https://github.com/mathieucarbou/license-maven-plugin/issues/431 – Jin Kwon Nov 27 '22 at 09:20

1 Answers1

5

Any comment end with ; cause sonar lint warning.

i just removed ; from Licensed under the Apache License, Version 2.0 (the "License"); and sonar lint warning gone.

Snehal Patel
  • 1,282
  • 2
  • 11
  • 25
  • 1
    If their check for code is "line ends in semicolon", that's super gross. – John Hascall Aug 25 '17 at 11:11
  • You are absolutely right.It's bad logic to identify commented out code.even this code issue a warning. `//comment text goes here ;` – Snehal Patel Aug 25 '17 at 11:13
  • I think that's the case, as a colleague found out and told me a few days ago, and I can reproduce & observe that. Upvoted, good to know! – Hugues M. Aug 25 '17 at 11:14
  • 2
    Yep. A trailing `;` or `{` or `}` yields a "score" of 0.95 (see [JavaFootprint.java](https://github.com/SonarSource/sonar-java/blob/4.12.0.11033/java-checks/src/main/java/org/sonar/java/checks/JavaFootprint.java#L37)), and the default threshold is 0.9 (see [CommentedOutCodeLineCheck.java](https://github.com/SonarSource/sonar-java/blob/4.12.0.11033/java-checks/src/main/java/org/sonar/java/checks/CommentedOutCodeLineCheck.java#L42)). I think trailing `;` should have lower score (so that alone would not trigger the rule). – Hugues M. Aug 25 '17 at 11:29
  • Thx @SnehalPatel I accept your answer but I don't accept SonarLint's way to handling the trailing `;`, I guess SonarLint needs a small piece of AI to get their job done better ;-) – Gelin Luo Aug 26 '17 at 11:38
  • @green : feel free to forward improvement suggestions on the [sonarqube group](https://groups.google.com/forum/#!forum/sonarqube) and further discuss this with SonarJava folks (analyzer used in this case) – Nicolas B. Aug 28 '17 at 07:12