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

linter: added checks for functions #605

Closed
wants to merge 6 commits into from
Closed

linter: added checks for functions #605

wants to merge 6 commits into from

Conversation

i582
Copy link
Contributor

@i582 i582 commented Jul 22, 2020

  1. Added check for bare returns for functions with non-void return types. For non-void functions, forbid bare return #563
  2. Added check for the absence of return statement in function with @return annotation, which is not equal to void. Find mismatching @return annotations #561

For this, the storage of CurrentFunction has been improved.
CurrentFunction now stores the fully qualified name of the function with its class and namespace. A separate CurrentMethod field was created to store the names of the methods. #597

Fixes #561
Fixes #563
Fixes #597

i582 and others added 3 commits July 21, 2020 16:19
Added check for bare returns for functions with non-void return types.
Added check for the absence of return statement in function with @return annotation, which is not equal to void.

For this, the storage of CurrentFunction has been improved.
CurrentFunction now stores the fully qualified name of the function with its class and namespace. A separate CurrentMethod field was created to store the names of the methods.
// Required to make a decision in void vs null type selection,
// since "return" is the same as "return null".
bareReturn bool
// when the function contains at least one return or yield.
containsReturnOrYield bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exitFlags should already contain this information in the way you want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to know if there is a return or yield in the body of the function in order to accurately determine "mismatchingReturn". I'm not sure that exitFlags holds the information I need. Comment mentions other things // if function has exit / die / throw, then ExitFlags will be <> 0.

src/linter/block.go Outdated Show resolved Hide resolved
@@ -698,7 +698,12 @@ func (d *RootWalker) handleFuncStmts(params []meta.FuncParam, uses, stmts []node
prematureExitFlags = cleanFlags
}

// if the function contains only one statement and this statement is throw.
oneThrowStatementFunction := b.containsThrow && len(stmts) == 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not two? Ten? Why this special case only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in basic_test.go:

// Functions of this kind are very common in Symfony, so it makes sense not to consider this 
// a loss of return, although in fact the @return annotation is not entirely correct.

@@ -52,6 +52,21 @@ MAYBE phpdoc: Missing PHPDoc for "getHelperName" public method at testdata/mus
MAYBE phpdoc: Missing PHPDoc for "getTemplateName" public method at testdata/mustache/src/Mustache/Exception/UnknownTemplateException.php:34
public function getTemplateName()
^^^^^^^^^^^^^^^
WARNING bareReturn: Replace 'return' with 'return null' at testdata/mustache/src/Mustache/Parser.php:207
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return; and return null; already mean the same thing. Maybe provide more context here on why exactly do you ask to replace it in this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we can provide additional information after the first sentence?
For example:

<critical> WARNING bareReturn: Replace 'return' with 'return null'. The '\f' function has @return annotation with non-'void' value, so 'return null' is preferred. at C:/projects/noverify/example/index.php:7
    return;
    ^^^^^^^

i582 added 3 commits July 22, 2020 16:15
The message for the 'bareReturn' error has been expanded. Added comment why replacement is needed.

Fixed crash bug baseline-test.
Removed the unnecessary check for the '\r' suffix in the report function.
@i582
Copy link
Contributor Author

i582 commented Aug 12, 2020

Closed until better times.

@i582 i582 closed this Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants