Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest avoiding default case in switch statements #481

Merged
merged 2 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
*
* 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.
*/

package com.palantir.baseline.errorprone;

import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.Category;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.SwitchTree;
import java.util.Objects;

@AutoService(BugChecker.class)
@BugPattern(
name = "SwitchStatementDefaultCase",
category = Category.ONE_OFF,
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a sentence or two to the README to flesh out the reasoning behind this a bit more (i.e. recommending to throw an unreachable exception)?

linkType = LinkType.CUSTOM,
severity = SeverityLevel.SUGGESTION,
summary = "Avoid default cases in switch statements to correctly handle new enum variants")
public final class SwitchStatementDefaultCase extends BugChecker implements BugChecker.SwitchTreeMatcher {

@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
if (hasDefaultCase(tree)) {
return buildDescription(tree)
.setMessage("Avoid using default case in switch statement.")
.build();
}

return Description.NO_MATCH;
}

private boolean hasDefaultCase(SwitchTree tree) {
return tree.getCases().stream()
.map(CaseTree::getExpression)
.anyMatch(Objects::isNull);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
*
* 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.
*/

package com.palantir.baseline.errorprone;

import com.google.errorprone.CompilationTestHelper;
import org.junit.Before;
import org.junit.Test;

public class SwitchStatementDefaultCaseTest {

private CompilationTestHelper compilationHelper;

@Before
public void before() {
compilationHelper = CompilationTestHelper.newInstance(SwitchStatementDefaultCase.class, getClass());
}

@Test
public void testDefaultCase() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" enum Case { ONE, TWO, THREE }",
" void m(Case c) {",
" // BUG: Diagnostic contains: Avoid using default case in switch statement.",
" switch (c) {",
" case ONE:",
" case TWO:",
" case THREE:",
" default:",
" break;",
" }",
" }",
"}")
.doTest();
}

@Test
public void testNoDefaultCase() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" enum Case { ONE, TWO, THREE }",
" void m(Case c) {",
" switch (c) {",
" case ONE:",
" case TWO:",
" case THREE:",
" break;",
" }",
" }",
"}")
.doTest();
}
}