From b411a731fef9af1c0a73e4d5b718e5a6b12af1c2 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Wed, 19 Jan 2022 19:46:17 +0000 Subject: Add nodiscard to core math classes to catch c++ errors. A common source of errors is to call functions (such as round()) expecting them to work in place, but them actually being designed only to return the processed value. Not using the return value in this case in indicative of a bug, and can be flagged as a warning by using the [[nodiscard]] attribute. --- core/math/aabb.h | 2 +- core/math/basis.h | 2 +- core/math/color.h | 2 +- core/math/face3.h | 2 +- core/math/plane.h | 2 +- core/math/quaternion.h | 2 +- core/math/rect2.h | 4 ++-- core/math/transform_2d.h | 2 +- core/math/transform_3d.h | 2 +- core/math/vector2.h | 4 ++-- core/math/vector3.h | 2 +- core/math/vector3i.h | 2 +- core/typedefs.h | 11 +++++++++++ editor/import/resource_importer_scene.cpp | 3 ++- platform/osx/display_server_osx.mm | 6 +++--- scene/main/canvas_item.cpp | 4 ++-- scene/resources/immediate_mesh.cpp | 2 +- servers/rendering/renderer_rd/renderer_scene_render_rd.cpp | 4 ++-- 18 files changed, 35 insertions(+), 23 deletions(-) diff --git a/core/math/aabb.h b/core/math/aabb.h index 2eaaafa27d..3d19410ddf 100644 --- a/core/math/aabb.h +++ b/core/math/aabb.h @@ -41,7 +41,7 @@ */ class Variant; -class AABB { +class _NO_DISCARD_ AABB { public: Vector3 position; Vector3 size; diff --git a/core/math/basis.h b/core/math/basis.h index 709f2cb3cf..802da82089 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -34,7 +34,7 @@ #include "core/math/quaternion.h" #include "core/math/vector3.h" -class Basis { +class _NO_DISCARD_ Basis { private: void _set_diagonal(const Vector3 &p_diag); diff --git a/core/math/color.h b/core/math/color.h index 6c09f7941c..72a4a5f8be 100644 --- a/core/math/color.h +++ b/core/math/color.h @@ -34,7 +34,7 @@ #include "core/math/math_funcs.h" #include "core/string/ustring.h" -struct Color { +struct _NO_DISCARD_ Color { union { struct { float r; diff --git a/core/math/face3.h b/core/math/face3.h index 5a34858ccb..3dbbca09e0 100644 --- a/core/math/face3.h +++ b/core/math/face3.h @@ -36,7 +36,7 @@ #include "core/math/transform_3d.h" #include "core/math/vector3.h" -class Face3 { +class _NO_DISCARD_ Face3 { public: enum Side { SIDE_OVER, diff --git a/core/math/plane.h b/core/math/plane.h index bac946502b..8cb6f62b3b 100644 --- a/core/math/plane.h +++ b/core/math/plane.h @@ -35,7 +35,7 @@ class Variant; -class Plane { +class _NO_DISCARD_ Plane { public: Vector3 normal; real_t d = 0; diff --git a/core/math/quaternion.h b/core/math/quaternion.h index cf3762e090..2575d7d229 100644 --- a/core/math/quaternion.h +++ b/core/math/quaternion.h @@ -36,7 +36,7 @@ #include "core/math/vector3.h" #include "core/string/ustring.h" -class Quaternion { +class _NO_DISCARD_ Quaternion { public: union { struct { diff --git a/core/math/rect2.h b/core/math/rect2.h index f34550bef1..4ea24e8f88 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -35,7 +35,7 @@ struct Transform2D; -struct Rect2 { +struct _NO_DISCARD_ Rect2 { Point2 position; Size2 size; @@ -363,7 +363,7 @@ struct Rect2 { } }; -struct Rect2i { +struct _NO_DISCARD_ Rect2i { Point2i position; Size2i size; diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index 752a885eba..6c2d51bd9b 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -33,7 +33,7 @@ #include "core/math/rect2.h" // also includes vector2, math_funcs, and ustring -struct Transform2D { +struct _NO_DISCARD_ Transform2D { // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper": // M = (elements[0][0] elements[1][0]) // (elements[0][1] elements[1][1]) diff --git a/core/math/transform_3d.h b/core/math/transform_3d.h index c0ef2ecfc1..c16c278e74 100644 --- a/core/math/transform_3d.h +++ b/core/math/transform_3d.h @@ -35,7 +35,7 @@ #include "core/math/basis.h" #include "core/math/plane.h" -class Transform3D { +class _NO_DISCARD_ Transform3D { public: Basis basis; Vector3 origin; diff --git a/core/math/vector2.h b/core/math/vector2.h index a340036ac7..af40b9e68d 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -36,7 +36,7 @@ struct Vector2i; -struct Vector2 { +struct _NO_DISCARD_ Vector2 { static const int AXIS_COUNT = 2; enum Axis { @@ -284,7 +284,7 @@ typedef Vector2 Point2; /* INTEGER STUFF */ -struct Vector2i { +struct _NO_DISCARD_ Vector2i { enum Axis { AXIS_X, AXIS_Y, diff --git a/core/math/vector3.h b/core/math/vector3.h index d7a72b05a8..b62edef40f 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -37,7 +37,7 @@ #include "core/string/ustring.h" class Basis; -struct Vector3 { +struct _NO_DISCARD_ Vector3 { static const int AXIS_COUNT = 3; enum Axis { diff --git a/core/math/vector3i.h b/core/math/vector3i.h index 1416c98057..1564ee9173 100644 --- a/core/math/vector3i.h +++ b/core/math/vector3i.h @@ -35,7 +35,7 @@ #include "core/string/ustring.h" #include "core/typedefs.h" -struct Vector3i { +struct _NO_DISCARD_ Vector3i { enum Axis { AXIS_X, AXIS_Y, diff --git a/core/typedefs.h b/core/typedefs.h index e6034eb375..5929b5123b 100644 --- a/core/typedefs.h +++ b/core/typedefs.h @@ -71,6 +71,17 @@ #endif #endif +// No discard allows the compiler to flag warnings if we don't use the return value of functions / classes +#ifndef _NO_DISCARD_ +#define _NO_DISCARD_ [[nodiscard]] +#endif + +// In some cases _NO_DISCARD_ will get false positives, +// we can prevent the warning in specific cases by preceding the call with a cast. +#ifndef _ALLOW_DISCARD_ +#define _ALLOW_DISCARD_ (void) +#endif + // Windows badly defines a lot of stuff we'll never use. Undefine it. #ifdef _WIN32 #undef min // override standard definition diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index b6624a8cfa..940fc442fb 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -1776,7 +1776,8 @@ void ResourceImporterScene::_optimize_track_usage(AnimationPlayer *p_player, Ani if (bone_idx == -1) { continue; } - skel->get_bone_pose(bone_idx); + // Note that this is using get_bone_pose to update the bone pose cache. + _ALLOW_DISCARD_ skel->get_bone_pose(bone_idx); loc = skel->get_bone_pose_position(bone_idx); rot = skel->get_bone_pose_rotation(bone_idx); scale = skel->get_bone_pose_scale(bone_idx); diff --git a/platform/osx/display_server_osx.mm b/platform/osx/display_server_osx.mm index e3b4333ec8..60f1eac4b1 100644 --- a/platform/osx/display_server_osx.mm +++ b/platform/osx/display_server_osx.mm @@ -316,7 +316,7 @@ static NSCursor *_cursorFromSelector(SEL selector, SEL fallback = nil) { CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y }; CGWarpMouseCursorPosition(lMouseWarpPos); } else { - _get_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]); + _ALLOW_DISCARD_ _get_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]); Input::get_singleton()->set_mouse_position(wd.mouse_pos); } @@ -1391,7 +1391,7 @@ inline void sendPanEvent(DisplayServer::WindowID window_id, double dx, double dy double deltaX, deltaY; - _get_mouse_pos(wd, [event locationInWindow]); + _ALLOW_DISCARD_ _get_mouse_pos(wd, [event locationInWindow]); deltaX = [event scrollingDeltaX]; deltaY = [event scrollingDeltaY]; @@ -2463,7 +2463,7 @@ void DisplayServerOSX::window_set_position(const Point2i &p_position, WindowID p [wd.window_object setFrameTopLeftPoint:NSMakePoint(position.x - offset.x, position.y - offset.y)]; _update_window(wd); - _get_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]); + _ALLOW_DISCARD_ _get_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]); } void DisplayServerOSX::window_set_max_size(const Size2i p_size, WindowID p_window) { diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp index 59d933fa1d..a0916c6291 100644 --- a/scene/main/canvas_item.cpp +++ b/scene/main/canvas_item.cpp @@ -1017,8 +1017,8 @@ void CanvasItem::set_notify_transform(bool p_enable) { notify_transform = p_enable; if (notify_transform && is_inside_tree()) { - //this ensures that invalid globals get resolved, so notifications can be received - get_global_transform(); + // This ensures that invalid globals get resolved, so notifications can be received. + _ALLOW_DISCARD_ get_global_transform(); } } diff --git a/scene/resources/immediate_mesh.cpp b/scene/resources/immediate_mesh.cpp index b9469803a0..28afef8638 100644 --- a/scene/resources/immediate_mesh.cpp +++ b/scene/resources/immediate_mesh.cpp @@ -382,7 +382,7 @@ AABB ImmediateMesh::get_aabb() const { if (i == 0) { aabb = surfaces[i].aabb; } else { - aabb.merge(surfaces[i].aabb); + aabb = aabb.merge(surfaces[i].aabb); } } return aabb; diff --git a/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp b/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp index 33f5a178e0..126f40584a 100644 --- a/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp @@ -514,7 +514,7 @@ Ref RendererSceneRenderRD::environment_bake_panorama(RID p_env, bool p_ba ambient_color_sky_mix = env->ambient_sky_contribution; const float ambient_energy = env->ambient_light_energy; ambient_color = env->ambient_light; - ambient_color.to_linear(); + ambient_color = ambient_color.to_linear(); ambient_color.r *= ambient_energy; ambient_color.g *= ambient_energy; ambient_color.b *= ambient_energy; @@ -533,7 +533,7 @@ Ref RendererSceneRenderRD::environment_bake_panorama(RID p_env, bool p_ba } else { const float bg_energy = env->bg_energy; Color panorama_color = ((environment_background == RS::ENV_BG_CLEAR_COLOR) ? storage->get_default_clear_color() : env->bg_color); - panorama_color.to_linear(); + panorama_color = panorama_color.to_linear(); panorama_color.r *= bg_energy; panorama_color.g *= bg_energy; panorama_color.b *= bg_energy; -- cgit v1.2.3