MultipleParallelOrSequentialCallsMultiple calls to either parallel or sequential are unnecessary and cause confusion.

Severity

WARNING

Has Fix?

REQUIRES_HUMAN_ATTENTION

The problem

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MultipleParallelOrSequentialCalls") to the enclosing element.

Positive examples

MultipleParallelOrSequentialCallsPositiveCases.java

/*
* Copyright 2017 The Error Prone Authors.
*
* 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.
*/packagecom.google.errorprone.bugpatterns.testdata;importjava.util.Collection;importjava.util.List;importjava.util.stream.Stream;/** @author @mariasam (Maria Sam) on 7/6/17. */classMultipleParallelOrSequentialCallsPositiveCases{publicvoidbasicCaseParallel(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel();'?list.stream().parallel().parallel();}publicvoidbasicCaseParallelNotFirst(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel().map(m -> m);'?list.stream().map(m->m).parallel().parallel();}publicvoidbasicCollection(Collection<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel();'?list.stream().parallel().parallel();}publicvoidparallelStream(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.parallelStream();'?list.parallelStream().parallel().parallel();}publicvoidbasicCaseParallelThisInMethodArg(List<String>list){// BUG: Diagnostic contains: Did you mean 'this.hello(list.stream().parallel());'?this.hello(list.stream().parallel().parallel());}publicvoidonlyOneError(List<String>list){this.hello(// BUG: Diagnostic contains: Multiple callslist.stream().parallel().parallel());}publicvoidmapMethod(List<String>list){// BUG: Diagnostic contains: Did you mean 'hello(list.stream().parallel().map(m ->// this.hello(null)));'?hello(list.stream().map(m->this.hello(null)).parallel().parallel());}publicvoidbetweenMethods(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel().map(m -> m.toString());'?list.stream().parallel().map(m->m.toString()).parallel();}publicvoidbasicCaseParallelNotLast(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel().map(m ->// m.toString()).findFirst();'?list.stream().parallel().map(m->m.toString()).parallel().findFirst();}publicvoidbasicCaseSequential(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().sequential().map(m -> m.toString());'?list.stream().sequential().map(m->m.toString()).sequential();}publicvoidbothSequentialAndParallel(List<String>list){// this case is unlikely (wrong, even) but just checking that this works// BUG: Diagnostic contains: Did you mean 'list.stream().sequential().parallel();'?list.stream().sequential().parallel().sequential();}publicvoidbothSequentialAndParallelMultiple(List<String>list){// this is even more messed up, this test is here to make sure the checker doesn't throw an// exception// BUG: Diagnostic contains: Multiple callslist.stream().sequential().parallel().sequential().parallel();}publicvoidparallelMultipleLines(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel()list.stream().parallel().map(m->m.toString()).parallel();}publicvoidmultipleParallelCalls(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.parallelStream();'?list.parallelStream().sequential();}publicStringhello(Streamst){return"";}publicvoidstreamWithinAStream(List<String>list,List<String>list2){// BUG: Diagnostic contains: Did you meanlist.stream().flatMap(childDir->list2.stream()).parallel().flatMap(a->list2.stream()).parallel();}publicvoidstreamWithinAStreamImmediatelyAfterOtherParallel(List<String>list,List<String>list2){// BUG: Diagnostic contains: Did you meanlist.stream().parallel().map(m->list2.stream().parallel()).parallel();}publicvoidparallelAndNestedStreams(List<String>list,List<String>list2){// BUG: Diagnostic contains: Did you meanlist.parallelStream().flatMap(childDir->list2.stream()).parallel().filter(m->(newTestClass("test")).testClass()).map(a->{if(a==null){returna;}returnnull;}).filter(a->a!=null).flatMap(a->list2.stream()).parallel();}privateclassTestClass{publicTestClass(Stringcon){}privatebooleantestClass(){returntrue;}}}

MultipleParallelOrSequentialCallsPositiveCases_expected.java

/*
* Copyright 2017 The Error Prone Authors.
*
* 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.
*/packagecom.google.errorprone.bugpatterns.testdata;importjava.util.Collection;importjava.util.List;importjava.util.stream.Stream;/** @author @mariasam (Maria Sam) on 7/6/17. */classMultipleParallelOrSequentialCallsPositiveCases{publicvoidbasicCaseParallel(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel();'?list.stream().parallel();}publicvoidbasicCaseParallelNotFirst(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel().map(m -> m);'?list.stream().parallel().map(m->m);}publicvoidbasicCollection(Collection<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel();'?list.stream().parallel();}publicvoidparallelStream(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.parallelStream();'?list.parallelStream();}publicvoidbasicCaseParallelThisInMethodArg(List<String>list){// BUG: Diagnostic contains: Did you mean 'this.hello(list.stream().parallel());'?this.hello(list.stream().parallel());}publicvoidonlyOneError(List<String>list){this.hello(// BUG: Diagnostic contains: Multiple callslist.stream().parallel());}publicvoidmapMethod(List<String>list){// BUG: Diagnostic contains: Did you mean 'hello(list.stream().parallel().map(m ->// this.hello(null)));'?hello(list.stream().parallel().map(m->this.hello(null)));}publicvoidbetweenMethods(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel().map(m -> m.toString());'?list.stream().parallel().map(m->m.toString());}publicvoidbasicCaseParallelNotLast(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel().map(m ->// m.toString()).findFirst();'?list.stream().parallel().map(m->m.toString()).findFirst();}publicvoidbasicCaseSequential(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().sequential().map(m -> m.toString());'?list.stream().sequential().map(m->m.toString());}publicvoidbothSequentialAndParallel(List<String>list){// this case is unlikely (wrong, even) but just checking that this works// BUG: Diagnostic contains: Did you mean 'list.stream().sequential().parallel();'?list.stream().sequential().parallel();}publicvoidbothSequentialAndParallelMultiple(List<String>list){// this is even more messed up, this test is here to make sure the checker doesn't throw an// exception// BUG: Diagnostic contains: Multiple callslist.stream().sequential().parallel().parallel();}publicvoidparallelMultipleLines(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.stream().parallel()list.stream().parallel().map(m->m.toString());}publicvoidmultipleParallelCalls(List<String>list){// BUG: Diagnostic contains: Did you mean 'list.parallelStream();'?list.parallelStream();}publicStringhello(Streamst){return"";}publicvoidstreamWithinAStream(List<String>list,List<String>list2){// BUG: Diagnostic contains: Did you meanlist.stream().parallel().flatMap(childDir->list2.stream()).flatMap(a->list2.stream());}publicvoidstreamWithinAStreamImmediatelyAfterOtherParallel(List<String>list,List<String>list2){// BUG: Diagnostic contains: Did you meanlist.stream().parallel().map(m->list2.stream().parallel());}publicvoidparallelAndNestedStreams(List<String>list,List<String>list2){// BUG: Diagnostic contains: Did you meanlist.parallelStream().flatMap(childDir->list2.stream()).filter(m->(newTestClass("test")).testClass()).map(a->{if(a==null){returna;}returnnull;}).filter(a->a!=null).flatMap(a->list2.stream());}privateclassTestClass{publicTestClass(Stringcon){}privatebooleantestClass(){returntrue;}}}

Negative examples

MultipleParallelOrSequentialCallsNegativeCases.java

/*
* Copyright 2017 The Error Prone Authors.
*
* 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.
*/packagecom.google.errorprone.bugpatterns.testdata;importjava.util.List;/** Created by mariasam on 7/6/17. */publicclassMultipleParallelOrSequentialCallsNegativeCases{publicvoidbasicCase(List<String>list){list.stream().parallel();}publicvoidbasicCaseSequential(List<String>list){list.stream().sequential();}publicvoidbasicCaseNotLast(List<String>list){list.stream().parallel().findFirst();}publicvoidmiddleParallel(List<String>list){list.stream().map(m->m).parallel().filter(m->m.isEmpty());}publicvoidotherMethod(){SomeObjectsomeObject=newSomeObject();someObject.parallel().parallel();}publicvoidotherMethodNotParallel(List<String>list){list.stream().filter(m->m.isEmpty()).findFirst();}publicvoidstreamWithinAStreamImmediatelyAfter(List<String>list){list.stream().map(m->list.stream().parallel()).parallel();}publicvoidstreamWithinAStreamImmediatelyAfterOtherParallelBothFirstAndWithin(List<String>list){list.stream().parallel().map(m->list.stream().parallel());}publicvoidstreamWithinAStreamImmediatelyAfterOtherParallelBoth(List<String>list){list.stream().sequential().map(m->list.stream().parallel()).parallel();}classSomeObject{publicSomeObjectparallel(){returnnull;}}}