A function that only computes a value is called and its value is thrown away

tx0/go:discarded-resultdefaultwarningbug

What it finds

strings.TrimSpace(s) as a statement: the function returns a new value and changes nothing else, so a call whose result is not used does nothing at all. The value was usually meant to be assigned, as in s = strings.TrimSpace(s).

The function must be one of a list of functions of the standard library that have no effects: every function of strings, bytes, strconv, unicode, unicode/utf8 other than EncodeRune, which writes into its argument, path, math and math/bits, the functions of path/filepath other than Walk and WalkDir, which call back, the formatting functions of fmt that return a string or an error, and a few of errors, time, net/url, html, reflect and sort. The package is resolved through the imports of the file. Only expression statements are reported; a defer or go of such a call is odd but may be deliberate.

From the rule's source, discarded_result.rs.

Example from the rule's tests
package p import (	"fmt"	"path/filepath"	"strings") func f(s string, n int) string {	strings.TrimSpace(s)tx0 reports: the result of strings.TrimSpace is not used	fmt.Sprintf("%d", n)tx0 reports: the result of fmt.Sprintf is not used	filepath.Join(s, "x")tx0 reports: the result of filepath.Join is not used	filepath.Walk(s, nil)	fmt.Println(s)	s = strings.TrimSpace(s)	_ = strings.ToUpper(s)	defer strings.ToLower(s)	return s} func g(s string) {	strings := local{}	strings.TrimSpace(s)} type local struct{} func (local) TrimSpace(string) string { return "" }
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/go:discarded-result" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:discarded-result] reason="..."

tx0 explain tx0/go:discarded-result shows the same in the terminal. Found a false positive? Report it.