implicit_return
- Value
trueorfalse- Default
true
A pattern return e also matches e where e is returned without the return keyword:
- the last expression of a function, in languages where that expression is the function's value, such as Ruby, Rust, Scala and Julia, including the last expression of each branch that ends the function;
- the body of a lambda, such as a Python
lambdaor a Kotlin lambda.
With implicit_return: false, only an explicit return matches.
Examples
The last expression of a Ruby method
The rule returns-user turns the option off.
users.yaml
rules: - id: returns-user-default pattern: return User.find(...) message: returns a user languages: [ruby] severity: WARNING - id: returns-user pattern: return User.find(...) message: returns a user languages: [ruby] severity: WARNING options: implicit_return: falseusers.rb
def find_user(id) # ruleid: returns-user-default User.find(id)enddef find_user_explicitly(id) # ruleid: returns-user-default, returns-user return User.find(id)enddef find_user_if_given(id) if id # ruleid: returns-user-default User.find(id) else nil endenddef find_and_log(id) # ok: returns-user-default user = User.find(id) log(user)endCommand and result
$ opengrep test .
2/2: ✓ All tests passed
No tests for fixes found.