-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1748
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!-- | ||
~ Copyright 2015-2017 Alexandr Evstigneev | ||
~ | ||
~ 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. | ||
--> | ||
|
||
<html> | ||
<body> | ||
<p>Port of SawyerX's <a href="https://github.com/xsawyerx/perl-critic-policy-variables-prohibitlooponhash"><code>Perl::Critic::Policy::Variables::ProhibitLoopOnHash</code></a> | ||
</p> | ||
|
||
<p>When "looping over hashes," we mean looping over hash keys or hash values. If you forgot to call keys or values you will accidentally | ||
loop over both.</p> | ||
|
||
<code> | ||
foreach my $foo (%hash) {...} # not ok | ||
action() for %hash; # not ok | ||
foreach my $foo ( keys %hash ) {...} # ok | ||
action() for values %hash; # ok | ||
</code> | ||
|
||
<p>An effort is made to detect expressions:</p> | ||
|
||
<code> | ||
action() for %hash ? keys %hash : (); # ok | ||
action() for %{ $hash{'stuff'} } ? keys %{ $hash{'stuff'} } : (); # ok | ||
</code> | ||
|
||
<p>(Granted, the second example there doesn't make much sense, but I have found a variationo of it in real code.)</p> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/com/perl5/lang/perl/idea/inspections/PerlHashLoopInspection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2015-2017 Alexandr Evstigneev | ||
* | ||
* 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.perl5.lang.perl.idea.inspections; | ||
|
||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiElementVisitor; | ||
import com.intellij.psi.tree.TokenSet; | ||
import com.intellij.psi.util.PsiUtilCore; | ||
import com.perl5.PerlBundle; | ||
import com.perl5.lang.perl.psi.PerlVisitor; | ||
import com.perl5.lang.perl.psi.PsiPerlConditionExpr; | ||
import com.perl5.lang.perl.psi.PsiPerlForCompound; | ||
import com.perl5.lang.perl.psi.PsiPerlForStatementModifier; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import static com.perl5.lang.perl.lexer.PerlElementTypesGenerated.HASH_CAST_EXPR; | ||
import static com.perl5.lang.perl.lexer.PerlElementTypesGenerated.HASH_VARIABLE; | ||
|
||
public class PerlHashLoopInspection extends PerlInspection { | ||
private static final TokenSet HASH_LIKE_TOKENS = TokenSet.create( | ||
HASH_VARIABLE, | ||
HASH_CAST_EXPR | ||
); | ||
|
||
@NotNull | ||
@Override | ||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { | ||
return new PerlVisitor() { | ||
@Override | ||
public void visitForStatementModifier(@NotNull PsiPerlForStatementModifier o) { | ||
checkExpression(o.getExpr()); | ||
} | ||
|
||
@Override | ||
public void visitForCompound(@NotNull PsiPerlForCompound o) { | ||
PsiPerlConditionExpr conditionExpr = o.getConditionExpr(); | ||
if (conditionExpr != null) { | ||
checkExpression(conditionExpr.getExpr()); | ||
} | ||
} | ||
|
||
private void checkExpression(@Nullable PsiElement expr) { | ||
if (HASH_LIKE_TOKENS.contains(PsiUtilCore.getElementType(expr))) { | ||
registerProblem(holder, expr, PerlBundle.message("perl.inspection.loop.on.hash")); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use strict; | ||
use warnings; | ||
use v5.10; | ||
|
||
my %hash; | ||
my $scalar; | ||
my @array; | ||
foreach my $element (<error descr="Hash iterating. You've probably missed 'keys' or 'values'">%hash</error>) {} | ||
for my $element (<error descr="Hash iterating. You've probably missed 'keys' or 'values'">%hash</error>) {} | ||
say for <error descr="Hash iterating. You've probably missed 'keys' or 'values'">%hash</error>; | ||
say foreach <error descr="Hash iterating. You've probably missed 'keys' or 'values'">%hash</error>; | ||
|
||
foreach my $element (<error descr="Hash iterating. You've probably missed 'keys' or 'values'">%$scalar</error>) {} | ||
for my $element (<error descr="Hash iterating. You've probably missed 'keys' or 'values'">%$scalar</error>) {} | ||
say for <error descr="Hash iterating. You've probably missed 'keys' or 'values'">%$scalar</error>; | ||
say foreach <error descr="Hash iterating. You've probably missed 'keys' or 'values'">%$scalar</error>; | ||
|
||
foreach my $element (<error descr="Hash iterating. You've probably missed 'keys' or 'values'">%{say 'hi'}</error>) {} | ||
for my $element (<error descr="Hash iterating. You've probably missed 'keys' or 'values'">%{say 'hi'}</error>) {} | ||
say for <error descr="Hash iterating. You've probably missed 'keys' or 'values'">%{say 'hi'}</error>; | ||
say foreach <error descr="Hash iterating. You've probably missed 'keys' or 'values'">%{say 'hi'}</error>; | ||
|
||
|
||
foreach my $element (@hash{1}) {} | ||
for my $element (@hash{1}) {} | ||
say for @hash{1}; | ||
say foreach @hash{1}; | ||
|
||
foreach my $element (@array) {} | ||
for my $element (@array) {} | ||
say for @array; | ||
say foreach @array; | ||
|