Git Product home page Git Product logo

error-prone's Introduction

Error Prone

Error Prone is a static analysis tool for Java that catches common programming mistakes at compile-time.

public class ShortSet {
  public static void main (String[] args) {
    Set<Short> s = new HashSet<>();
    for (short i = 0; i < 100; i++) {
      s.add(i);
      s.remove(i - 1);
    }
    System.out.println(s.size());
  }
}
error: [CollectionIncompatibleType] Argument 'i - 1' should not be passed to this method;
its type int is not compatible with its collection's type argument Short
      s.remove(i - 1);
              ^
    (see https://errorprone.info/bugpattern/CollectionIncompatibleType)
1 error

Getting Started

Our documentation is at errorprone.info.

Error Prone works with Bazel, Maven, Ant, and Gradle. See our installation instructions for details.

Developing Error Prone

Developing and building Error Prone is documented on the wiki.

Links

error-prone's People

Contributors

alexeagle avatar amalloy avatar awturner avatar billpugh avatar cgdecker avatar cpovirk avatar cushon avatar dependabot[bot] avatar dimo414 avatar eaftan avatar eamonnmcmanus avatar epmjohnston avatar gk5885 avatar graememorgan avatar java-team-github-bot avatar kevin1e100 avatar kluever avatar konne88 avatar lowasser avatar mdempsky avatar msridhar avatar netdpb avatar nick-someone avatar rickie avatar ronshapiro avatar sgoldfed avatar sjnickerson avatar stephan202 avatar sumitbhagwani avatar supertriceratops avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

error-prone's Issues

Code review request

Original issue created by [email protected] on 2012-10-01 at 10:34 PM


I fixed a bug with the compiler in the case that we are trying to scan a compilation unit that hasn't had all of its classes attributed yet.

Previously I had assumed that flow() would be called once on each class; that turns out not to be the case. We now track which classes error-prone has seen, and we only scan a compilation unit when we've seen all of its contained classes.

Types generation from strings.

Original issue created by [email protected] on 2012-07-27 at 11:14 AM


As far as I dug into the error-prone and javac there is no convenient way to get a Type from String. And sometimes you need to check if the result type is a subtype of/castable to java.lang.Object[] or java.util.List<Integer>, etc.

Of course for plain classes (e.g. java.lang.Object) you can make a look-up in symtab, but you can't do the same for array types and parametrized classes. (Am I wrong here?)

I think it would be great to have such a method in error-prone library.
Something like "Type getType(String typeName)".

Printed error messages are sometimes incorrect

Original issue created by [email protected] on 2012-01-10 at 12:55 AM


When a fix is to delete a line, but the line is not the same one where the error was found, the error message is incorrect. For example, this code:

if (i == 10)
  ;
{
  foo...
}

Will produce the following error message:

[Empty if] Empty statement after if; did you mean to remove this line?
    if (i == 10)
    ^

When you intend it to delete the line with the semicolon.

I've added a test case in emptyifstatement/PositiveCases.java under test5().

errorMessageTemplates in Guava Preconditions class can only accept "%s" format variables

Original issue created by [email protected] on 2012-10-24 at 06:06 PM


Several methods in the Guava Preconditions class take an errorMessageTemplate that may be customized by substituting for "%s" in the template. Often the templates actually include other format specifiers ("%d", "%f", etc.), but those are not supported by the formatter.

Example:
Preconditions.checkArgument(i > 0, "i (%d) must be greater than zero", i);
should be
Preconditions.checkArgument(i > 0, "i (%s) must be greater than zero", i);

Code review request

Original issue created by [email protected] on 2011-10-21 at 05:58 PM


If you have some time, I've made some more commits which are unreviewed and I'd be interested in feedback.

Like we just discussed, there are still cases where the Exception reference escapes and this code reports a false positive error.

GWT SafeHtml check

Original issue created by [email protected] on 2012-07-19 at 08:35 PM


GWT has an API called "SafeHtml" that has certain documented restrictions on how you're supposed to use it, and assuming developers use it correctly they should be largely protected against XSS vulnerabilities. Using it correctly amounts to two requirements:

  1. Some methods like SafeHtmlUtils.fromSafeConstant(String) are documented as requiring a "safe" string literal as the argument. Safe is defined here as a string that correctly parses as a sequence of complete HTML tags and leaves the parser in standard HTML context. (E.g., "<b>" is okay; "<a href='" and "<script>" are not.)

  2. GWT has legacy methods like Element.setInnerHTML(String) that predate the introduction of SafeHtml APIs and subvert the protections offered, so they should be avoided in favor of the SafeHtml-variants.

My FindBugs works by recognizing the following expressions as safe:

  1. String literals that return true for com.google.gwt.safehtml.shared.SafeHtmlHostedModeUtils.isCompleHtml(String).
  2. String values returned from a call to SafeHtml.asString().
  3. A concatenation of string expressions recognized as safe (e.g., "<b> + safeHtmlValue.asString() + "</b>" is still safe).

It then warns about calls to methods like SafeHtmlUtils.fromSafeConstant(String) or Element.setInnerHTML(String) that aren't using safe arguments. Recognizing "safe" expressions this way is intentionally lenient to avoid needless code churn due to obviously safe (and incredibly common) code constructs like element.setInnerHTML("").

(It's complicated somewhat further because there are some methods that have an interface like setTextOrHTML(String text, boolean isHtml), and I further only warn on these methods if I can't statically determine the isHtml boolean argument is always false.)

Filler for Non-Existent Issue

Filler for non-existent Google Code issue 22.

This issue only exists to ensure that GitHub issues have the same IDs they had on Google Code. Please ignore it.

Code review request

Original issue created by [email protected] on 2011-09-27 at 10:30 PM


Branch name:
fixmultipleimports

Purpose of code changes on this branch:
Fixed two problems: (1) Matcher now checks all imports, and (2) matcher now supports fully-qualified method calls.

When reviewing my code changes, please focus on:

After the review, I'll merge this branch into:
master

AST is missing comments

Original issue created by [email protected] on 2012-01-30 at 05:20 PM


When we scan an AST, the comments are missing. This prevents us checking file contents in comments, and also means that our suggested fix/refactoring loses comments in the tree being modified.

Code review request

Original issue created by [email protected] on 2012-07-26 at 09:54 PM


Please take a look at my 3 commits from today (7/26). I reworked ErrorReportingJavaCompiler to scan compilation units all at once, so that file-level nodes like imports and package declarations are scanned.

Disallow annotating a non-static method with @BeforeClass

Original issue created by [email protected] on 2012-07-16 at 07:18 PM


The JUnit @BeforeClass annotation should only be applied to a public static void no-arg method, otherwise it throws a runtime error. We should detect this at compile time.

There may also be other JUnit-specific checks we can write. For example, JUnit doesn't run tests that are not marked public. This can cause someone to mistakenly think that their newly created tests are runnign when they're actually not. Some other reason tests weren't being run:

  • The test class did not extend TestCase
  • The test method wasn't public
  • The test method didn't start with "test" (e.g. tesSomething)
  • The test method took a parameter

Code review request

Original issue created by [email protected] on 2011-10-28 at 09:26 AM


Purpose of code changes on this branch:

Checking and fixing String.format() calls in Guava Preconditions calls.

When reviewing my code changes, please focus on:

General code review - I'm a beginner to this code, so I'm probably doing things in a non-optimal way.

After the review, I'll merge this branch into:
master

Release 0.9 has bad tools jar dependency

Original issue created by [email protected] on 2012-05-30 at 04:31 AM


The maven release automatically resolved a variable from my local path, which isn't valid outside of my machine.
The current 0.9 release has this in the POM:
<dependencies>
    <dependency>
      <groupId>openjdk</groupId>
      <artifactId>tools</artifactId>
      <version>1.6</version>
      <scope>system</scope>
      <systemPath>/usr/local/buildtools/java/jdk6-google-v4/jre/../lib/tools.jar</systemPath>
    </dependency>

Check that equals() and hashCode() read the same fields

Original issue created by [email protected] on 2012-08-06 at 08:48 PM


If I add a new field to a class, I can remember to update equals() but forget to update hashCode(). How would we detect this? In many cases, both methods should read all fields of a class, but that's probably too strong a check. (For example, a List.equals() implementation might read no fields directly, preferring to operation on the public size() and get() methods.) A weaker but probably still useful check is that the two methods read the same set of fields. False positives are still possible. For example, the check would flag a field containing a cached hash code, which would likely be read in hashCode() but not equals(). This particular example is avoidable by requiring the hashCode() looks at a subset of the fields that equals() looks at. I conjecture that it's more common for programmers to update equals() but forget to update hashCode() than vice versa, so this further weakened check would likely still catch most problems.

Code review request

Original issue created by [email protected] on 2011-11-30 at 04:40 PM


Purpose of code changes:
Added a empty-if-statements checker

When reviewing my code changes, please focus on:
Did I construct the new matchers correctly? Some of the type signatures were confusing. Everything does work.

Code review request

Original issue created by [email protected] on 2011-09-26 at 11:56 PM


Branch name:
importfix

Purpose of code changes on this branch:
Fixed a failing test case where the Preconditions.checkNotNull() method being called was a different one from the one the check should match. Fixed this by altering the system to process import statements.

After the review, I'll merge this branch into:
master

Support Apple-provided JDK

Original issue created by [email protected] on 2012-05-21 at 03:22 AM


Currently, the way we hook into javac has only been tested with OpenJDK 6 and 7. Not sure how many developers expect to compile with Apple's JDK, since it is probably going to be sunset and Oracle will start maintaining the Mac version.

Code review request

Original issue created by [email protected] on 2012-09-11 at 11:21 AM


Branch name: longliteral

Purpose of code changes on this branch: Add checker for long literals ending with lower case ell, e.g. 123432l rather than 123432L. See #23

After the review, I'll merge this branch into: /master

Code review request

Original issue created by [email protected] on 2012-08-29 at 03:39 PM


Better suggested fix and test case for the Ordering.from refactoring:

Ordering.from(new Comparator<T>() { ... })

to

new Ordering<T>() { ... }

There might be a nicer way of getting hold of the "..." than the way I've found (which is basically to re-construct the AST object and pretty-print it): hints welcome.

Code review request

Original issue created by [email protected] on 2012-09-04 at 10:55 AM


Branch name: suppress

Purpose of code changes on this branch:

Checks for incorrectly spelled @SuppressWarnings() annotation values ("deprecated" instead of "deprecation" - cleaned it up recently with several dozen instances in google3).

I've refactored some of the code from the FallThroughSuppression check
into an abstract superclass.

After the review, I'll merge this branch into:
/master

Code review request

Original issue created by [email protected] on 2012-10-18 at 09:42 AM


Branch name: unneeded-ternary

Purpose of code changes on this branch:
Add new check for an unnecessary conditional operator

After the review, I'll merge this branch into:
/trunk

Validate literal regular expressions

Original issue created by [email protected] on 2012-10-19 at 02:10 AM


When a string literal is passed for a parameter that is known to expect a regex (Pattern.compile, String.split, etc.), try to actually compile that regex, and fail the build if that throws an exception.

(Perhaps also when the value of some constant is passed, and that constant is set to a literal. Etc.)

Run checks in Eclipse

Original issue created by [email protected] on 2012-01-29 at 07:56 PM


ECJ (eclipse compiler for Java) has its own representation of the source. We need to abstract our usage of the AST and symbol table to be able to operate on this representation. Then, we need to figure out where to wire in the execution of our TreeScanners.

Fallthrough suppression shouldn't be on by default

Original issue created by [email protected] on 2012-05-24 at 04:29 PM


Tried turning on error prone in the Guava maven build.

Get lots of errors like:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project guava: Compilation failure: Compilation failure:
[ERROR] /Users/alexeagle/guava-libraries/guava/src/com/google/common/math/LongMath.java:[74,3] [FallthroughSuppression] Fallthrough warning suppression has no effect if warning is suppressed
[ERROR](see http://code.google.com/p/error-prone/wiki/FallthroughSuppression)
[ERROR] did you mean to remove this line?

Filler for Non-Existent Issue

Filler for non-existent Google Code issue 21.

This issue only exists to ensure that GitHub issues have the same IDs they had on Google Code. Please ignore it.

private Fields which should not be reassigned, but cannot be marked with final keyword

Original issue created by [email protected] on 2012-07-30 at 05:15 PM


Suggested by David Mankin:

In Guice, we have @Inject fields, which should only be set by the injector when creating the class. Since they can't be marked final, we often prefer to use constructor injection instead, even though it's quite bulky.

Similarly in GWT, fields cannot be marked final because the serialization mechanism needs to be able to set the values in the server when populating a data object.

We should be able to enforce that such fields are never re-assigned in code within the class.

We might want an annotation for this, maybe in JSR305, or maybe JSR330 (which would be nice since other DI frameworks should take advantage). For Guice, you could imagine @Inject(finalish=true) but if we do GWT, may as well make an annotation.

Probably not practical to do this with non-private fields since we'd have to scan outside the enclosing class.

Add PreconditionsTooManyArgs bug pattern

Original issue created by [email protected] on 2012-10-26 at 10:47 PM


Branch name: preconditions-toomanyargs

Purpose of code changes on this branch:
Add a new bug pattern to match (especially) cases in which the wrong Precondition formatting placeholders have been used.

When reviewing my code changes, please focus on:
I have no idea what I'm doing.

After the review, I'll merge this branch into:
/trunk

Doesn't work on OpenJDK

Original issue created by [email protected] on 2012-01-30 at 05:19 PM


Our compile is broken against OpenJDK right now.

Additionally, the javac.util.Messages API in jdk6 has become javac.util.JavacMessages in jdk7, so we'll need to compile correctly against either version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.