Skip to content

Commit

Permalink
add -check-return support too (experimental ... too many notices)
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Nov 27, 2024
1 parent 6695b85 commit 994f502
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
15 changes: 12 additions & 3 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -3965,9 +3965,18 @@ fn (mut c Checker) resolve_return_type(node ast.CallExpr) ast.Type {
}

fn (mut c Checker) check_must_use_call_result(node &ast.CallExpr, f &ast.Fn, label string) {
if node.is_return_used || !f.is_must_use {
if node.is_return_used {
return
}
c.warn('return value must be used, ${label} `${f.name}` was tagged with `@[must_use]`',
node.pos)
if f.return_type == ast.void_type {
return
}
if f.is_must_use {
c.warn('return value must be used, ${label} `${f.name}` was tagged with `@[must_use]`',
node.pos)
return
}
if c.pref.is_check_return {
c.note('return value must be used', node.pos)
}
}
13 changes: 10 additions & 3 deletions vlib/v/help/build/build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ NB: the build flags are shared with the run command too:
Define the provided flag.
If `value` is not provided, it is assumed to be set to `true`.
`value` can be any pure literal like `32` (i64),`34.98` (f64), `false` (bool),
`v` (char) or `"V rocks"` (string). In V code you can use `value := $d('<flag>', <default value>)`
to retrieve the value assigned to `<flag>`.
`v` (char) or `"V rocks"` (string).
In V code you can use `value := $d('<flag>', <default value>)` to retrieve the
value assigned to `<flag>`.
If no flag identifier (or value) is assigned, `$d()` will return the passed `<default value>`.

-g
Expand Down Expand Up @@ -245,10 +246,16 @@ NB: the build flags are shared with the run command too:
NB: in the future, this will be turned ON by default,
and will become an error, after vlib modules are cleaned up.

-check-return
Note about all calls, that ignore the return value of
the corresponding fn/method.
NB: this is still experimental, the rules for it will
change, it may be dropped completely, or it may become the default.

For C-specific build flags, use `v help build-c`.
For JS-specific build flags, use `v help build-js`.
For Native-specific build flags, use `v help build-native`.
For WebAssembly-specific build flags, use `v help build-wasm`.

See also:
`v help run` for documentation regarding `v run`.
`v help run` for documentation regarding `v run`.
4 changes: 4 additions & 0 deletions vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub mut:
is_callstack bool // turn on callstack registers on each call when v.debug is imported
is_trace bool // turn on possibility to trace fn call where v.debug is imported
is_coverage bool // turn on code coverage stats
is_check_return bool // -check-return, will make V produce notices about *all* call expressions with unused results. NOTE: experimental!
eval_argument string // `println(2+2)` on `v -e "println(2+2)"`. Note that this source code, will be evaluated in vsh mode, so 'v -e 'println(ls(".")!)' is valid.
test_runner string // can be 'simple' (fastest, but much less detailed), 'tap', 'normal'
profile_file string // the profile results will be stored inside profile_file
Expand Down Expand Up @@ -956,6 +957,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
'-check-unused-fn-args' {
res.show_unused_params = true
}
'-check-return' {
res.is_check_return = true
}
'-use-coroutines' {
res.use_coroutines = true
$if macos || linux {
Expand Down

0 comments on commit 994f502

Please sign in to comment.