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

refactor(gyro_odometer): apply static analysis #7360

Conversation

a-maumau
Copy link
Contributor

@a-maumau a-maumau commented Jun 7, 2024

Description

This PR applies some suggestions from the linter to localization/gyro_odometer.

Fixed:

  • camelCase to snake_case
  • use explicit cast
  • rename
  • use initialization list for some variable (in test)
  • removed default destructor
  • rename ambiguous function name (collide with some library)
  • remove "using declaration" in header file

Tests performed

Checked with clang-tidy and cppcheck (v2.14.1)

check_linter.sh
#!/bin/bash
set -eux

TARGET_DIR=$1

current_dir=$(basename $(pwd))
if [[ ! $current_dir =~ ^(autoware|pilot-auto) ]]; then
    echo "This script must be run in a directory with a prefix of autoware or pilot-auto."
    exit 1
fi

set +eux
export CPLUS_INCLUDE_PATH=/usr/include/c++/11:/usr/include/x86_64-linux-gnu/c++/11:$CPLUS_INCLUDE_PATH
set -eux

fdfind -e cpp -e hpp --full-path ${TARGET_DIR} | xargs -P $(nproc) -I{} cpplint {}
fdfind -e cpp -e hpp --full-path ${TARGET_DIR} | xargs -P $(nproc) -I{} clang-tidy -p build/ {}

Before fixing the code:

output from above commands
$ ./check_linter.sh gyro_odometer
...
13232 warnings generated.
Suppressed 13243 warnings (13232 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
13229 warnings generated.
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/test/test_gyro_odometer_helper.hpp:23:1: warning: using declarations in the global namespace in headers are prohibited [google-global-names-in-headers]
using geometry_msgs::msg::TwistWithCovarianceStamped;
^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/test/test_gyro_odometer_helper.hpp:24:1: warning: using declarations in the global namespace in headers are prohibited [google-global-names-in-headers]
using sensor_msgs::msg::Imu;
^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/test/test_gyro_odometer_helper.hpp:26:5: warning: invalid case style for function 'generateSampleImu' [readability-identifier-naming]
Imu generateSampleImu();
    ^~~~~~~~~~~~~~~~~
    generate_sample_imu
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/test/test_gyro_odometer_helper.hpp:27:28: warning: invalid case style for function 'generateSampleVelocity' [readability-identifier-naming]
TwistWithCovarianceStamped generateSampleVelocity();
                           ^~~~~~~~~~~~~~~~~~~~~~
                           generate_sample_velocity
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/test/test_gyro_odometer_helper.hpp:28:21: warning: invalid case style for function 'getNodeOptionsWithDefaultParams' [readability-identifier-naming]
rclcpp::NodeOptions getNodeOptionsWithDefaultParams();
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    get_node_options_with_default_params
Suppressed 13235 warnings (13224 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
13245 warnings generated.
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/diagnostics_module.hpp:33:8: warning: invalid case style for function 'addKeyValue' [readability-identifier-naming]
  void addKeyValue(const diagnostic_msgs::msg::KeyValue & key_value_msg);
       ^~~~~~~~~~~
       add_key_value
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/diagnostics_module.hpp:35:8: warning: invalid case style for function 'addKeyValue' [readability-identifier-naming]
  void addKeyValue(const std::string & key, const T & value);
       ^~~~~~~~~~~
       add_key_value
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/diagnostics_module.hpp:36:8: warning: invalid case style for function 'updateLevelAndMessage' [readability-identifier-naming]
  void updateLevelAndMessage(const int8_t level, const std::string & message);
       ^~~~~~~~~~~~~~~~~~~~~
       update_level_and_message
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/diagnostics_module.hpp:40:3: warning: function 'createDiagnosticsArray' should be marked [[nodiscard]] [modernize-use-nodiscard]
  diagnostic_msgs::msg::DiagnosticArray createDiagnosticsArray(
  ^
  [[nodiscard]] 
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/diagnostics_module.hpp:40:41: warning: invalid case style for function 'createDiagnosticsArray' [readability-identifier-naming]
  diagnostic_msgs::msg::DiagnosticArray createDiagnosticsArray(
                                        ^~~~~~~~~~~~~~~~~~~~~~
                                        create_diagnostics_array
Suppressed 13251 warnings (13240 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
13799 warnings generated.
Suppressed 13810 warnings (13799 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
14510 warnings generated.
Suppressed 14538 warnings (14510 in non-user code, 28 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
15508 warnings generated.
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/gyro_odometer_core.hpp:43:7: warning: class 'GyroOdometerNode' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions]
class GyroOdometerNode : public rclcpp::Node
      ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/gyro_odometer_core.hpp:50:3: error: annotate this function with 'override' or (rarely) 'final' [modernize-use-override,-warnings-as-errors]
  ~GyroOdometerNode();
  ^
                      override
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/gyro_odometer_core.hpp:53:8: warning: invalid case style for function 'callbackVehicleTwist' [readability-identifier-naming]
  void callbackVehicleTwist(
       ^~~~~~~~~~~~~~~~~~~~
       callback_vehicle_twist
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/gyro_odometer_core.hpp:55:8: warning: invalid case style for function 'callbackImu' [readability-identifier-naming]
  void callbackImu(const sensor_msgs::msg::Imu::ConstSharedPtr imu_msg_ptr);
       ^~~~~~~~~~~
       callback_imu
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/gyro_odometer_core.hpp:56:8: warning: invalid case style for function 'concatGyroAndOdometer' [readability-identifier-naming]
  void concatGyroAndOdometer();
       ^~~~~~~~~~~~~~~~~~~~~
       concat_gyro_and_odometer
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/gyro_odometer_core.hpp:57:8: warning: invalid case style for function 'publishData' [readability-identifier-naming]
  void publishData(const geometry_msgs::msg::TwistWithCovarianceStamped & twist_with_cov_raw);
       ^~~~~~~~~~~
       publish_data
Suppressed 15513 warnings (15502 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
1 warning treated as error
19912 warnings generated.
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/test/test_gyro_odometer_pubsub.cpp:65:6: warning: invalid case style for function 'spinSome' [readability-identifier-naming]
void spinSome(rclcpp::Node::SharedPtr node_ptr)
     ^~~~~~~~
     spin_some
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/test/test_gyro_odometer_pubsub.cpp:73:6: warning: invalid case style for function 'isTwistValid' [readability-identifier-naming]
bool isTwistValid(
     ^~~~~~~~~~~~
     is_twist_valid
Suppressed 19938 warnings (19910 in non-user code, 28 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
20162 warnings generated.
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/gyro_odometer_core.hpp:53:8: warning: function 'autoware::gyro_odometer::GyroOdometerNode::callbackVehicleTwist' has a definition with different parameter names [readability-inconsistent-declaration-parameter-name]
  void callbackVehicleTwist(
       ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:83:24: note: the definition seen here
void GyroOdometerNode::callbackVehicleTwist(
                       ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/include/gyro_odometer/gyro_odometer_core.hpp:53:8: note: differing parameters are named here: ('vehicle_twist_msg_ptr'), in definition: ('vehicle_twist_ptr')
  void callbackVehicleTwist(
       ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:34:23: warning: invalid case style for function 'transformCovariance' [readability-identifier-naming]
std::array<double, 9> transformCovariance(const std::array<double, 9> & cov)
                      ^~~~~~~~~~~~~~~~~~~
                      transform_covariance
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:40:3: warning: uninitialized record type: 'cov_transformed' [cppcoreguidelines-pro-type-member-init]
  std::array<double, 9> cov_transformed;
  ^
                                       {}
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:79:19: error: use '= default' to define a trivial destructor [modernize-use-equals-default,-warnings-as-errors]
GyroOdometerNode::~GyroOdometerNode()
                  ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:226:14: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
  vx_mean /= vehicle_twist_queue_.size();
             ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:227:29: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
  vx_covariance_original /= vehicle_twist_queue_.size();
                            ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:237:18: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
  gyro_mean.x /= gyro_queue_.size();
                 ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:238:18: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
  gyro_mean.y /= gyro_queue_.size();
                 ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:239:18: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
  gyro_mean.z /= gyro_queue_.size();
                 ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:240:33: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
  gyro_covariance_original.x /= gyro_queue_.size();
                                ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:241:33: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
  gyro_covariance_original.y /= gyro_queue_.size();
                                ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:242:33: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
  gyro_covariance_original.z /= gyro_queue_.size();
                                ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:260:30: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
    vx_covariance_original / vehicle_twist_queue_.size();
                             ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:264:34: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
    gyro_covariance_original.x / gyro_queue_.size();
                                 ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:266:34: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
    gyro_covariance_original.y / gyro_queue_.size();
                                 ^
/home/masakibaba/autoware/src/universe/autoware.universe/localization/gyro_odometer/src/gyro_odometer_core.cpp:268:34: warning: narrowing conversion from 'std::deque::size_type' (aka 'unsigned long') to 'double' [cppcoreguidelines-narrowing-conversions]
    gyro_covariance_original.z / gyro_queue_.size();
                                 ^
Suppressed 20157 warnings (20146 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
1 warning treated as error

Check with cppcheck:

output
$ cppcheck --enable=warning --enable=style --enable=performance --enable=portability --enable=unusedFunction --inconclusive --check-level=exhaustive .

Checking src/diagnostics_module.cpp ...
1/5 files checked 15% done
Checking src/gyro_odometer_core.cpp ...
Checking src/gyro_odometer_core.cpp: ROS_DISTRO_GALACTIC...
2/5 files checked 67% done
Checking test/test_gyro_odometer_helper.cpp ...
3/5 files checked 73% done
Checking test/test_gyro_odometer_pubsub.cpp ...
test/test_gyro_odometer_pubsub.cpp:57:9: performance: Variable 'received_latest_twist_ptr' is assigned in constructor body. Consider performing initialization in initialization list. [useInitializationList]
        received_latest_twist_ptr = msg;
        ^
4/5 files checked 96% done
Checking test/test_main.cpp ...
5/5 files checked 100% done


After this PR:

Check with check_linter.sh

$ ./check_linter.sh gyro_odometer
...
13240 warnings generated.
Suppressed 13251 warnings (13240 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
13227 warnings generated.
Suppressed 13238 warnings (13227 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
13224 warnings generated.
Suppressed 13235 warnings (13224 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
14510 warnings generated.
Suppressed 14538 warnings (14510 in non-user code, 28 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
13794 warnings generated.
Suppressed 13805 warnings (13794 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
15497 warnings generated.
Suppressed 15508 warnings (15497 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
19896 warnings generated.
Suppressed 19924 warnings (19896 in non-user code, 28 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
20134 warnings generated.
Suppressed 20145 warnings (20134 in non-user code, 11 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

Check with cppcheck (no warnings):

$ cppcheck --enable=warning --enable=style --enable=performance --enable=portability --enable=unusedFunction --inconclusive --check-level=exhaustive .

...
5/5 files checked 100% done

Effects on system behavior

Not applicable.

Interface changes

Pre-review checklist for the PR author

The PR author must check the checkboxes below when creating the PR.

In-review checklist for the PR reviewers

The PR reviewers must check the checkboxes below before approval.

Post-review checklist for the PR author

The PR author must check the checkboxes below before merging.

  • There are no open discussions or they are tracked via tickets.

After all checkboxes are checked, anyone who has write access can merge the PR.

@github-actions github-actions bot added the component:localization Vehicle's position determination in its environment. (auto-assigned) label Jun 7, 2024
@SakodaShintaro SakodaShintaro added the tag:run-build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci) label Jun 7, 2024
Copy link
Contributor

@SakodaShintaro SakodaShintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have confirmed that logging_simulator works well.
Thank you so much!
Looks Good To Me

@SakodaShintaro SakodaShintaro enabled auto-merge (squash) June 7, 2024 08:44
@SakodaShintaro SakodaShintaro merged commit 2b0de17 into autowarefoundation:main Jun 7, 2024
35 of 37 checks passed
KhalilSelyan pushed a commit that referenced this pull request Jul 22, 2024
* refactor based on linter

Signed-off-by: a-maumau <[email protected]>

* style(pre-commit): autofix

---------

Signed-off-by: a-maumau <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@a-maumau a-maumau deleted the mau/refactor/localization/gyro_odometer branch August 1, 2024 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:localization Vehicle's position determination in its environment. (auto-assigned) tag:run-build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants