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

Allow more fine-grained suppression of ConstructorInvokesOverridable #656

Closed
Stephan202 opened this issue Jun 30, 2017 · 1 comment
Closed

Comments

@Stephan202
Copy link
Contributor

Consider the following code:

final class Container {
    @SuppressWarnings("ConstructorInvokesOverridable")
    abstract static class Test1 {
        private final Class<?> type = myType();

        abstract Class<?> myType();
    }

    abstract static class Test2 {
        @SuppressWarnings("ConstructorInvokesOverridable")
        private final Class<?> type = myType();

        abstract Class<?> myType();
    }

    abstract static class Test3 {
        @SuppressWarnings("ConstructorInvokesOverridable")
        Test3() {
            myType();
        }

        abstract Class<?> myType();
    }
}

Running with -Xep:ConstructorInvokesOverridable:ERROR yields:

Container.java:11: error: [ConstructorInvokesOverridable] Constructors should not invoke overridable methods.
        private final Class<?> type = myType();
                                            ^
    (see http://errorprone.info/bugpattern/ConstructorInvokesOverridable)
Container.java:19: error: [ConstructorInvokesOverridable] Constructors should not invoke overridable methods.
            myType();
                  ^
    (see http://errorprone.info/bugpattern/ConstructorInvokesOverridable)
2 errors

As you can see, no error is emitted for Test1. But for the other two classes @SuppressWarnings("ConstructorInvokesOverridable") has no effect.

For classes with many fields or much code in initializer blocks/constructors, it'd be nice if ConstructorInvokesOverridable could be suppressed at a scope smaller than the whole class. (Refactoring the affected classes would be even better, of course, but alas.)

michaelhixson added a commit to michaelhixson/error-prone that referenced this issue Dec 15, 2019
Do not match methods of anonymous classes.  Those methods can't be
overriden because those classes can't be extended.

Do not match methods of certain enum classes.  It is not possible to
override the methods of an enum class except in the (anonymous) class
bodies of its enum constants.  If all the enum constants lack class
bodies, then the enum class's methods can't be overridden.

Fix cases where the bug pattern would become confused about the receiver
of unqualified or this-qualified method invocations, and where it would
wrongly conclude that the method belonged to the instance under
construction and issue a warning.

Support @SuppressWarnings("ConstructorInvokesOverridable") on more
elements.  Fixes google#656.
@Stephan202
Copy link
Contributor Author

A fix for this issue is part of #1449.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant