unused_string_operation.ex (1155B)
1 defmodule Credo.Check.Warning.UnusedStringOperation do 2 use Credo.Check, 3 base_priority: :high, 4 explanations: [ 5 check: """ 6 The result of a call to the String module's functions has to be used. 7 8 While this is correct ... 9 10 def salutation(username) do 11 username = String.downcase(username) 12 13 "Hi #\{username}" 14 end 15 16 ... we forgot to save the downcased username in this example: 17 18 # This is bad because it does not modify the username variable! 19 20 def salutation(username) do 21 String.downcase(username) 22 23 "Hi #\{username}" 24 end 25 26 Since Elixir variables are immutable, String operations never work on the 27 variable you pass in, but return a new variable which has to be used somehow. 28 """ 29 ] 30 31 alias Credo.Check.Warning.UnusedOperation 32 33 @checked_module :String 34 @funs_with_return_value nil 35 36 @doc false 37 @impl true 38 def run(%SourceFile{} = source_file, params) do 39 UnusedOperation.run( 40 source_file, 41 params, 42 @checked_module, 43 @funs_with_return_value, 44 &format_issue/2 45 ) 46 end 47 end