【问题标题】:Sonarlint complains about license header in my source fileSonarlint 抱怨我的源文件中的许可证头
【发布时间】:2018-02-03 02:20:14
【问题描述】:

这是我在源代码中的许可证头:

包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));
    }
    ...

当我运行SonarLint 时,它说文件中存在问题:

检查源代码时如何获取 SonarLint 跳过许可证标头块?

更新

SonarLint 版本:

【问题讨论】:

  • 出于某种原因,它似乎认为 cmets 包含代码。请edit您的帖子并将文件的前 30-40 行(不仅仅是许可证标题)复制/粘贴到您的帖子中,以及屏幕截图。
  • 您是否检查过 SonarLint 中是否存在未解决的错误?
  • 没有检查是否报告了错误。更新问题并添加到源代码文件的链接
  • 我们不鼓励链接到非现场资源,因为随着时间的推移,这个问题预计会独立存在。关键是将来成为其他人的资源,如果您的链接消失,问题就变得毫无用处。
  • 如果我理解正确,您的许可证在package 声明下方?你能把你的许可证放在你的文件上吗?这应该可以解决问题。

标签: java sonarqube sonarlint sonarlint-intellij


【解决方案1】:

任何以; 结尾的评论都会导致声纳棉绒警告。

我刚刚从Licensed under the Apache License, Version 2.0 (the "License"); 中删除了;,并且sonar lint 警告消失了。

【讨论】:

  • 如果他们对代码的检查是“行以分号结尾”,那就太恶心了。
  • 你是绝对正确的。识别注释掉的代码是错误的逻辑。即使这个代码也会发出警告。 //comment text goes here ;
  • 我认为是这样,正如一位同事几天前发现并告诉我的那样,我可以重现并观察到这一点。赞成,很高兴知道!
  • 是的。尾随 ;{} 产生的“分数”为 0.95(参见 JavaFootprint.java),默认阈值为 0.9(参见 CommentedOutCodeLineCheck.java)。我认为尾随; 应该有较低的分数(这样就不会触发规则)。
  • Thx @SnehalPatel 我接受你的回答,但我不接受 SonarLint 处理尾随 ; 的方式,我猜 SonarLint 需要一小块 AI 才能更好地完成工作;-)
猜你喜欢
  • 2012-12-11
  • 1970-01-01
  • 2014-02-03
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
相关资源
最近更新 更多