From: "jeremyevans0 (Jeremy Evans) via ruby-core" <ruby-core@...> Date: 2024-09-16T15:54:58+00:00 Subject: [ruby-core:119226] [Ruby master Feature#20742] Trying to assign to a variable in statement modifier should emit a warning Issue #20742 has been updated by jeremyevans0 (Jeremy Evans). Actually, the pattern is fairly common, I should have searched for `unless` usage in addition to `if`: ``` lib/rdoc/code_object/any_method.rb: return nil unless klass = @store.find_class_or_module(klass_name) lib/rdoc/code_object/class_module.rb: next unless cm = const.is_alias_for lib/rdoc/parser/c.rb: next unless cls = @classes[c] lib/rdoc/parser/c.rb: return {} unless files = @store.cache[map_name] lib/rdoc/parser/c.rb: return {} unless name_map = files[@file_name] lib/rdoc/parser/c.rb: next unless mod = @store.find_class_or_module(name) lib/rdoc/parser/ruby.rb: return unless signature = RDoc::TomDoc.signature(comment) lib/rdoc/parser/changelog.rb: return unless last = entry_body.last lib/rdoc/parser/prism_ruby.rb: return unless signature = RDoc::TomDoc.signature(comment) lib/rdoc/parser.rb: return nil unless type = $1 lib/rdoc/servlet.rb: return unless ims = req['if-modified-since'] lib/rdoc/store.rb: break unless name = @c_enclosure_names[variable] lib/rubygems/core_ext/kernel_warn.rb: next unless path = loc.path lib/rubygems/gem_runner.rb: return [] unless offset = args.index("--") lib/rubygems/bundler_version_finder.rb: return unless contents = lockfile_contents lib/rubygems/bundler_version_finder.rb: next unless gemfile = Gem::GEM_DEP_FILES.find {|f| File.file?(f) } lib/rubygems/request_set/gem_dependency_api.rb: return unless repository = options.delete(:git) lib/rubygems/request_set/gem_dependency_api.rb: return unless git_source = (@git_sources.keys & options.keys).last lib/rubygems/request_set/gem_dependency_api.rb: return unless directory = options.delete(:path) lib/rubygems/request_set/gem_dependency_api.rb: return unless source = options.delete(:source) lib/rubygems/vendor/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular.rb: return unless index = array.index(item) lib/rubygems/vendor/molinillo/lib/molinillo/dependency_graph/detach_vertex_named.rb: return [] unless @vertex = graph.vertices.delete(name) lib/rubygems/vendor/molinillo/lib/molinillo/dependency_graph/log.rb: return unless action = @current_action lib/rubygems/vendor/molinillo/lib/molinillo/resolution.rb: return unless index = @parents_of[requirement].last lib/rubygems/vendor/molinillo/lib/molinillo/resolution.rb: return unless parent_state = @states[index] lib/rubygems/vendor/molinillo/lib/molinillo/resolution.rb: return nil unless vertex = activated.vertex_named(name) lib/prettyprint.rb: return unless group = @group_queue.deq lib/mkmf.rb: next unless signed = try_signedness(typedef, member, [prelude]) lib/bundler/cli.rb: return super unless command_path = Bundler.which("bundler-#{command}") lib/bundler/cli.rb: next unless o = cmd.options[k] lib/bundler/cli/open.rb: return unless spec = Bundler::CLI::Common.select_spec(name, :regex_match) lib/bundler/compact_index_client/updater.rb: return unless header = response["Repr-Digest"] || response["Digest"] lib/bundler/compact_index_client/updater.rb: next unless value = byte_sequence(value) lib/bundler/compact_index_client/parser.rb: return unless (name_end = line.index(" ")) # Artifactory bug causes blank lines in artifactor index files lib/bundler/compact_index_client/parser.rb: return unless (checksum_start = line.index(" ", name_end + 1) + 1) lib/bundler/dsl.rb: next unless param = opts[type] lib/bundler/fetcher.rb: return unless uri = connection.proxy_uri lib/bundler/installer/parallel_installer.rb: raise "failed to find a spec to enqueue while installing serially" unless spec_install = @specs.find(&:ready_to_enqueue?) lib/bundler/lockfile_generator.rb: return unless locked_ruby_version = definition.locked_ruby_version lib/bundler/lockfile_parser.rb: return unless spec = @specs[full_name] lib/bundler/runtime.rb: return unless activated_spec = Bundler.rubygems.loaded_specs(spec.name) lib/bundler/plugin/api/source.rb: next unless spec = Bundler.load_gemspec(file) lib/bundler/source.rb: return unless source_slug = extension_cache_slug(spec) lib/bundler/source/path.rb: return unless spec = Bundler.load_gemspec(file) lib/bundler/source/path.rb: next unless spec = load_gemspec(file) lib/bundler/source/rubygems.rb: return unless remote = spec.remote lib/bundler/source/rubygems.rb: return unless cache_slug = remote.cache_slug lib/bundler/source/rubygems.rb: return unless remote = spec.remote lib/bundler/spec_set.rb: break unless dep = deps.shift lib/bundler/checksum.rb: return unless source = gem_package.instance_variable_get(:@gem) lib/reline/io/windows.rb: return unless csbi = get_console_screen_buffer_info lib/reline/io/windows.rb: return unless csbi = get_console_screen_buffer_info lib/reline/io/windows.rb: return unless csbi = get_console_screen_buffer_info lib/reline/io/windows.rb: return unless csbi = get_console_screen_buffer_info ``` ---------------------------------------- Feature #20742: Trying to assign to a variable in statement modifier should emit a warning https://bugs.ruby-lang.org/issues/20742#change-109805 * Author: esad (Esad Hajdarevic) * Status: Open ---------------------------------------- There is an example in Control Expressions documentation: ``` p a if a = 0.zero? # raises NameError ���undefined local variable or method ���a������. ``` However, if we had already defined `a` there would be no exception raised. If one uses something like `p` for scratch variable, due to Kernel#p, also no exception is raised. Statement modifier is generally somewhat inverting the code flow (the right part is evaluated first then the left part), so it is not really obvious why binding variables shouldn't follow the same flow. A warning would be very beneficial. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/