From 5bea0666b48dfa0f5eaf894c214f73ac2e5dbc62 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Fri, 24 Jul 2026 20:08:46 -0700 Subject: [PATCH] Fix more MSVC warnings for potential mod by 0 (C4724) (cherry picked from commit 6f072b22896fee293b99100fd83d0b9e2a183058) --- core/string/string_name.cpp | 7 ++++--- editor/scene/2d/tiles/tile_data_editors.cpp | 17 ++++++++++------- scene/2d/physics/collision_polygon_2d.cpp | 11 ++++++----- scene/3d/lightmap_gi.cpp | 7 ++++--- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/core/string/string_name.cpp b/core/string/string_name.cpp index 5fc70a4c94..0892ad00b6 100644 --- a/core/string/string_name.cpp +++ b/core/string/string_name.cpp @@ -72,7 +72,8 @@ void StringName::cleanup() { data.sort_custom(); int unreferenced_stringnames = 0; int rarely_referenced_stringnames = 0; - for (int i = 0; i < data.size(); i++) { + const int data_size = data.size(); + for (int i = 0; i < data_size; i++) { print_line(itos(i + 1) + ": " + data[i]->name + " - " + itos(data[i]->debug_references)); if (data[i]->debug_references == 0) { unreferenced_stringnames += 1; @@ -81,8 +82,8 @@ void StringName::cleanup() { } } - print_line(vformat("\nOut of %d StringNames, %d StringNames were never referenced during this run (0 times) (%.2f%%).", data.size(), unreferenced_stringnames, unreferenced_stringnames / float(data.size()) * 100)); - print_line(vformat("Out of %d StringNames, %d StringNames were rarely referenced during this run (1-4 times) (%.2f%%).", data.size(), rarely_referenced_stringnames, rarely_referenced_stringnames / float(data.size()) * 100)); + print_line(vformat("\nOut of %d StringNames, %d StringNames were never referenced during this run (0 times) (%.2f%%).", data_size, unreferenced_stringnames, unreferenced_stringnames / float(data_size) * 100)); + print_line(vformat("Out of %d StringNames, %d StringNames were rarely referenced during this run (1-4 times) (%.2f%%).", data_size, rarely_referenced_stringnames, rarely_referenced_stringnames / float(data_size) * 100)); } #endif int lost_strings = 0; diff --git a/editor/scene/2d/tiles/tile_data_editors.cpp b/editor/scene/2d/tiles/tile_data_editors.cpp index 8fe2bf64a7..96e7597893 100644 --- a/editor/scene/2d/tiles/tile_data_editors.cpp +++ b/editor/scene/2d/tiles/tile_data_editors.cpp @@ -224,8 +224,9 @@ void GenericTilePolygonEditor::_base_control_draw() { base_control->draw_polygon(polygon, v_color); color.a = 0.7; - for (int j = 0; j < polygon.size(); j++) { - base_control->draw_line(polygon[j], polygon[(j + 1) % polygon.size()], color); + const int polygon_size = polygon.size(); + for (int j = 0; j < polygon_size; j++) { + base_control->draw_line(polygon[j], polygon[(j + 1) % polygon_size], color); } } @@ -437,9 +438,10 @@ void GenericTilePolygonEditor::_grab_polygon_segment_point(Vector2 p_pos, const float closest_distance = grab_threshold * 2.0; for (unsigned int i = 0; i < polygons.size(); i++) { const Vector &polygon = polygons[i]; - for (int j = 0; j < polygon.size(); j++) { + const int polygon_size = polygon.size(); + for (int j = 0; j < polygon_size; j++) { const Vector2 segment_a = polygon[j]; - const Vector2 segment_b = polygon[(j + 1) % polygon.size()]; + const Vector2 segment_b = polygon[(j + 1) % polygon_size]; Vector2 closest_point = Geometry2D::get_closest_point_to_segment(point, segment_a, segment_b); float distance = closest_point.distance_to(point); if (distance < grab_threshold / editor_zoom_widget->get_zoom() && distance < closest_distance) { @@ -463,7 +465,8 @@ void GenericTilePolygonEditor::_snap_to_tile_shape(Point2 &r_point, float &r_cur // Snap to polygon vertices. bool snapped = false; - for (int i = 0; i < polygon.size(); i++) { + const int polygon_size = polygon.size(); + for (int i = 0; i < polygon_size; i++) { float distance = r_point.distance_to(polygon[i]); if (distance < p_snap_dist && distance < r_current_snapped_dist) { snapped_point = polygon[i]; @@ -474,9 +477,9 @@ void GenericTilePolygonEditor::_snap_to_tile_shape(Point2 &r_point, float &r_cur // Snap to edges if we did not snap to vertices. if (!snapped) { - for (int i = 0; i < polygon.size(); i++) { + for (int i = 0; i < polygon_size; i++) { const Vector2 segment_a = polygon[i]; - const Vector2 segment_b = polygon[(i + 1) % polygon.size()]; + const Vector2 segment_b = polygon[(i + 1) % polygon_size]; Point2 point = Geometry2D::get_closest_point_to_segment(r_point, segment_a, segment_b); float distance = r_point.distance_to(point); if (distance < p_snap_dist && distance < r_current_snapped_dist) { diff --git a/scene/2d/physics/collision_polygon_2d.cpp b/scene/2d/physics/collision_polygon_2d.cpp index 61bcedadc5..44462be7d3 100644 --- a/scene/2d/physics/collision_polygon_2d.cpp +++ b/scene/2d/physics/collision_polygon_2d.cpp @@ -43,9 +43,10 @@ void CollisionPolygon2D::_build_polygon() { collision_object->shape_owner_clear_shapes(owner_id); bool solids = build_mode == BUILD_SOLIDS; + const int polygon_size = polygon.size(); if (solids) { - if (polygon.size() < 3) { + if (polygon_size < 3) { return; } @@ -59,19 +60,19 @@ void CollisionPolygon2D::_build_polygon() { } } else { - if (polygon.size() < 2) { + if (polygon_size < 2) { return; } Ref concave = memnew(ConcavePolygonShape2D); Vector segments; - segments.resize(polygon.size() * 2); + segments.resize(polygon_size * 2); Vector2 *w = segments.ptrw(); - for (int i = 0; i < polygon.size(); i++) { + for (int i = 0; i < polygon_size; i++) { w[(i << 1) + 0] = polygon[i]; - w[(i << 1) + 1] = polygon[(i + 1) % polygon.size()]; + w[(i << 1) + 1] = polygon[(i + 1) % polygon_size]; } concave->set_segments(segments); diff --git a/scene/3d/lightmap_gi.cpp b/scene/3d/lightmap_gi.cpp index 241bfe82d9..121c504984 100644 --- a/scene/3d/lightmap_gi.cpp +++ b/scene/3d/lightmap_gi.cpp @@ -1288,10 +1288,11 @@ LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_pa GenProbesOctree octree; octree.size = subdiv; - for (int i = 0; i < mesh_data.size(); i++) { + const int mesh_data_size = mesh_data.size(); + for (int i = 0; i < mesh_data_size; i++) { if (p_bake_step) { - float p = (float)(i) / mesh_data.size(); - p_bake_step(0.3 + p * 0.1, vformat(RTR("Creating probes from mesh %d/%d"), i, mesh_data.size()), p_bake_userdata, false); + float p = (float)(i) / mesh_data_size; + p_bake_step(0.3 + p * 0.1, vformat(RTR("Creating probes from mesh %d/%d"), i, mesh_data_size), p_bake_userdata, false); } for (int j = 0; j < mesh_data[i].points.size(); j += 3) {