openfoodfacts-server icon indicating copy to clipboard operation
openfoodfacts-server copied to clipboard

New Perl::Critic rules

Open dipietroR opened this issue 2 years ago • 9 comments

Describe the bug

Thinking of adding [BuiltinFunctions::RequireBlockMap], [BuiltinFunctions::RequireBlockGrep], and [InputOutput::RequireBriefOpen]

This would involve updating the code to match: https://metacpan.org/pod/Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap I'm not so clear on if we want to do this as it looks more like a readability thing than something performance based. Still having things in braces looks nice.

Another part of this would be: https://metacpan.org/pod/Perl::Critic::Policy::InputOutput::RequireBriefOpen Where we should close filehandles once we are done with them. For me, this one seems more valuable to do.

To Reproduce

Run perlcritic --severity 4

Expected behavior

Perlcritic would report file as ok.

Screenshots

No response

Additional context

For more context: https://metacpan.org/pod/Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap https://metacpan.org/pod/Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep https://metacpan.org/pod/Perl::Critic::Policy::InputOutput::RequireBriefOpen

Type of device

Other

Browser version

No response

Number of products impacted

No response

Time per product

No response

dipietroR avatar Jul 20 '22 23:07 dipietroR

For me the "RequireBriefOpen" might be the most interesting. But it may require to refactor lots of code part in a heavy way (eg. changing loops in file lines to isolate file iterations in an iterator function).

alexgarel avatar Jul 21 '22 09:07 alexgarel

Those rules sound good to me, we have very few map and grep, so it should be easy to do.

For the open files, I'm curious to see how many of them will be flagged. It may force use to refactor and create functions for the processing, but that's a good thing.

stephanegigandet avatar Jul 21 '22 09:07 stephanegigandet

"InputOutput" name space seems interesting.

We may also explore the "Variables" and "ValuesAndExpressions" rules. As they may have interesting rules. For example

alexgarel avatar Jul 21 '22 09:07 alexgarel

I wanted to find a rule that would spot the auto vivification or lack of variable declaration (we have this in Tags.pm, the variables used by load_taxonomy but couldn't find it !

alexgarel avatar Jul 21 '22 09:07 alexgarel

I looked at RequireBriefOpen when we were doing the severity 4 PR, and IIRC there were few subroutines that would need significant refactoring to pass the default close within 9 lines. I like the policy, but it's not trivial like the grep/map one.

Beside readability, block grep/map scales better because you can just add statements to the block.

Ban3 avatar Jul 21 '22 11:07 Ban3

These are the policies in the Perl-Critic distribution with the default severity of 4:

Out of these we have already disabled ProhibitMatchVars because they should not be an issue in newer Perls.

Ban3 avatar Jul 21 '22 13:07 Ban3

Sounds good. I'll start working on the grep, the map test, and ProhibitReusedNames. I agree looks like RequireBriefOpen would be hard to implement.

dipietroR avatar Jul 21 '22 14:07 dipietroR

These are the policies in the Perl-Critic distribution with the default severity of 4:

Thank you for giving a list of them all!

dipietroR avatar Jul 21 '22 15:07 dipietroR

Are there any other policies that pique anyone's interest?

dipietroR avatar Jul 21 '22 15:07 dipietroR