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: revive, Rule: string-format - Allows you to configure a list of regular expressions that string literals in certain function calls are checked against. Should we enable it? #15832

Closed
zak-pawel opened this issue Sep 2, 2024 · 0 comments · Fixed by #15983
Labels

Comments

@zak-pawel
Copy link
Collaborator

Description

This issue starts a discussion about enabling:

  • linter: revive - Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. Revive provides a framework for development of custom rules, and lets you define a strict preset for enhancing your development & code review processes.
  • rule: string-format - This rule allows you to configure a list of regular expressions that string literals in certain function calls are checked against.
    This is geared towards user facing applications where string literals are often used for messages that will be presented to users, so it may be desirable to enforce consistent formatting.

Configuration:

Each argument is a slice containing 2-3 strings: a scope, a regex, and an optional error message.

  1. The first string defines a scope. This controls which string literals the regex will apply to, and is defined as a function argument. It must contain at least a function name (core.WriteError). Scopes may optionally contain a number specifying which argument in the function to check (core.WriteError[1]), as well as a struct field (core.WriteError[1].Message, only works for top level fields). Function arguments are counted starting at 0, so [0] would refer to the first argument, [1] would refer to the second, etc. If no argument number is provided, the first argument will be used (same as [0]). You can use multiple scopes to one regex. Split them by , (core.WriteError,fmt.Errorf).

  2. The second string is a regular expression (beginning and ending with a / character), which will be used to check the string literals in the scope. The default semantics is "strings matching the regular expression are OK". If you need to inverse the semantics you can add a ! just before the first /. Examples:

    • with "/^[A-Z].*$/" the rule will accept strings starting with capital letters
    • with "!/^[A-Z].*$/" the rule will a fail on strings starting with capital letters
  3. The third string (optional) is a message containing the purpose for the regex, which will be used in lint errors.

Example configuration:

      - name: string-format
        arguments:
          - - 'fmt.Errorf[0]'
            - '/^([^A-Z]|$)/'
            - 'Error string must not start with a capital letter.'
          - - 'fmt.Errorf[0]'
            - '/(^|[^\.!?])$/'
            - 'Error string must not end in punctuation.'
          - - 'errors.New[0]'
            - '/^([^A-Z]|$)/'
            - 'Error string must not start with a capital letter.'
          - - 'errors.New[0]'
            - '/(^|[^\.!?])$/'
            - 'Error string must not end in punctuation.'
          - - 'panic'
            - '/^[^\n]*$/'
            - 'Must not contain line breaks.'

Expected output

Decision about enabling or not enabling this rule.

Findings

For this rule (with above configuration), the following findings were found in the current codebase:

cmd/telegraf/telegraf.go:346:21                                         revive  string-format: Error string must not end in punctuation.
cmd/telegraf/telegraf.go:349:21                                         revive  string-format: Error string must not end in punctuation.
internal/snmp/field.go:85:21                                            revive  string-format: Error string must not start with a capital letter.
internal/snmp/field.go:89:21                                            revive  string-format: Error string must not start with a capital letter.
internal/snmp/table.go:59:21                                            revive  string-format: Error string must not start with a capital letter.
plugins/common/shim/goshim.go:82:22                                     revive  string-format: Error string must not start with a capital letter.
plugins/common/shim/goshim.go:87:22                                     revive  string-format: Error string must not start with a capital letter.
plugins/common/shim/goshim.go:92:22                                     revive  string-format: Error string must not start with a capital letter.
plugins/common/socket/stream.go:103:21                                  revive  string-format: Error string must not start with a capital letter.
plugins/common/socket/stream.go:112:21                                  revive  string-format: Error string must not start with a capital letter.
plugins/inputs/activemq/activemq.go:158:26                              revive  string-format: Error string must not start with a capital letter.
plugins/inputs/bond/bond.go:140:20                                      revive  string-format: Error string must not start with a capital letter.
plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go:307:22        revive  string-format: Error string must not start with a capital letter.
plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go:327:31        revive  string-format: Error string must not start with a capital letter.
plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go:333:30        revive  string-format: Error string must not start with a capital letter.
plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go:755:29        revive  string-format: Error string must not start with a capital letter.
plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt_test.go:1064:38  revive  string-format: Error string must not start with a capital letter.
plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt_test.go:1125:38  revive  string-format: Error string must not start with a capital letter.
plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt_test.go:1125:83  revive  string-format: Error string must not start with a capital letter.
plugins/inputs/google_cloud_storage/google_cloud_storage.go:100:29      revive  string-format: Error string must not start with a capital letter.
plugins/inputs/google_cloud_storage/google_cloud_storage.go:241:21      revive  string-format: Error string must not start with a capital letter.
plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go:41:25       revive  string-format: Error string must not start with a capital letter.
plugins/inputs/intel_pmt/xml_parser.go:181:21                           revive  string-format: Error string must not start with a capital letter.
plugins/inputs/intel_pmt/xml_parser.go:257:21                           revive  string-format: Error string must not start with a capital letter.
plugins/inputs/iptables/iptables.go:85:27                               revive  string-format: Error string must not start with a capital letter.
plugins/inputs/kafka_consumer/kafka_consumer.go:134:21                  revive  string-format: Error string must not start with a capital letter.
plugins/inputs/linux_cpu/linux_cpu.go:187:21                            revive  string-format: Error string must not start with a capital letter.
plugins/inputs/nginx_plus_api/nginx_plus_api.go:77:28                   revive  string-format: Error string must not start with a capital letter.
plugins/inputs/opcua/read_client.go:142:21                              revive  string-format: Error string must not start with a capital letter.
plugins/inputs/sflow/packetdecoder.go:63:26                             revive  string-format: Error string must not start with a capital letter.
plugins/inputs/sflow/packetdecoder.go:77:26                             revive  string-format: Error string must not start with a capital letter.
plugins/inputs/sflow/packetdecoder.go:392:24                            revive  string-format: Error string must not start with a capital letter.
plugins/inputs/smartctl/smartctl.go:73:21                               revive  string-format: Error string must not start with a capital letter.
plugins/inputs/smartctl/smartctl.go:78:22                               revive  string-format: Error string must not start with a capital letter.
plugins/inputs/supervisor/supervisor.go:147:21                          revive  string-format: Error string must not start with a capital letter.
plugins/inputs/systemd_units/systemd_units_test.go:972:26               revive  string-format: Error string must not start with a capital letter.
plugins/inputs/tengine/tengine.go:55:28                                 revive  string-format: Error string must not start with a capital letter.
plugins/inputs/unbound/unbound.go:136:28                                revive  string-format: Error string must not start with a capital letter.
plugins/inputs/zipkin/codec/jsonV1/jsonV1.go:223:25                     revive  string-format: Error string must not start with a capital letter.
plugins/inputs/zipkin/codec/jsonV1/jsonV1.go:246:25                     revive  string-format: Error string must not start with a capital letter.
plugins/inputs/zipkin/handler.go:141:25                                 revive  string-format: Error string must not start with a capital letter.
plugins/inputs/zipkin/zipkin_test.go:651:21                             revive  string-format: Error string must not start with a capital letter.
plugins/inputs/zipkin/zipkin_test.go:658:21                             revive  string-format: Error string must not start with a capital letter.
plugins/outputs/mongodb/mongodb.go:100:22                               revive  string-format: Error string must not start with a capital letter.
plugins/outputs/mongodb/mongodb.go:103:22                               revive  string-format: Error string must not start with a capital letter.
plugins/outputs/nats/nats.go:271:22                                     revive  string-format: Error string must not start with a capital letter.
plugins/outputs/newrelic/newrelic.go:46:21                              revive  string-format: Error string must not start with a capital letter.
plugins/outputs/opentsdb/opentsdb.go:77:21                              revive  string-format: Error string must not start with a capital letter.
plugins/outputs/opentsdb/opentsdb.go:81:21                              revive  string-format: Error string must not start with a capital letter.
plugins/outputs/opentsdb/opentsdb.go:157:21                             revive  string-format: Error string must not start with a capital letter.
plugins/outputs/opentsdb/opentsdb.go:161:21                             revive  string-format: Error string must not start with a capital letter.
plugins/outputs/socket_writer/socket_writer.go:68:22                    revive  string-format: Error string must not start with a capital letter.
plugins/outputs/socket_writer/socket_writer.go:78:22                    revive  string-format: Error string must not start with a capital letter.
plugins/outputs/stackdriver/stackdriver_test.go:619:28                  revive  string-format: Error string must not start with a capital letter.
plugins/outputs/stackdriver/stackdriver_test.go:624:28                  revive  string-format: Error string must not start with a capital letter.
plugins/outputs/timestream/timestream.go:117:21                         revive  string-format: Error string must not start with a capital letter.
plugins/outputs/timestream/timestream.go:121:21                         revive  string-format: Error string must not start with a capital letter.
plugins/parsers/dropwizard/parser.go:101:25                             revive  string-format: Error string must not start with a capital letter.
plugins/parsers/influx/influx_upstream/parser.go:24:27                  revive  string-format: Error string must not start with a capital letter.
plugins/parsers/json/json_flattener.go:72:21                            revive  string-format: Error string must not start with a capital letter.
plugins/parsers/json/parser.go:95:22                                    revive  string-format: Error string must not start with a capital letter.
plugins/parsers/json/parser_test.go:843:30                              revive  string-format: Error string must not start with a capital letter.
plugins/parsers/json_v2/parser.go:230:27                                revive  string-format: Error string must not start with a capital letter.
plugins/parsers/json_v2/parser.go:477:27                                revive  string-format: Error string must not start with a capital letter.
plugins/parsers/json_v2/parser.go:649:25                                revive  string-format: Error string must not start with a capital letter.
plugins/parsers/nagios/parser_test.go:33:23                             revive  string-format: Error string must not start with a capital letter.
plugins/parsers/nagios/parser_test.go:36:24                             revive  string-format: Error string must not start with a capital letter.
plugins/parsers/wavefront/element.go:11:35                              revive  string-format: Error string must not start with a capital letter.
plugins/processors/snmp_lookup/lookup_test.go:30:42                     revive  string-format: Error string must not start with a capital letter.
plugins/processors/snmp_lookup/lookup_test.go:36:21                     revive  string-format: Error string must not start with a capital letter.
plugins/processors/snmp_lookup/lookup_test.go:52:20                     revive  string-format: Error string must not start with a capital letter.
plugins/processors/snmp_lookup/lookup_test.go:245:27                    revive  string-format: Error string must not start with a capital letter.
tools/config_includer/generator.go:31:25                                revive  string-format: Error string must not start with a capital letter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants