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

Fix MISRA C++ 2023 rule 15.1.3 "Conversion operators and constructors that are callable with a single argument shall be explicit" #593

Closed
mikir opened this issue Apr 12, 2024 · 3 comments
Assignees
Labels
bug Something isn't working c++ C++ language generator
Milestone

Comments

@mikir
Copy link
Contributor

mikir commented Apr 12, 2024

Zserio version and language
Zserio: latest from master
Language: C++

Describe the bug
Check and resolve SCA warning MISRACPP2023-15_1_3-a ("Conversion operators and constructors that are callable with a single argument shall be explicit") reported in Zserio on C++ runtime library.

Rule ID severity Message Path Line Link to sonarcloud
MISRACPP2023-15_1_3-a 2 Constructor 'inplace_optional_holder' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/OptionalHolder.h 670 https://sonarcloud.io/project/issues?open=AY3k4v9hXMcagTJBbG4m&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'inplace_optional_holder' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/OptionalHolder.h 678 https://sonarcloud.io/project/issues?open=AY3k4v9hXMcagTJBbG4o&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'inplace_optional_holder' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/OptionalHolder.h 689 https://sonarcloud.io/project/issues?open=AY3k4v9hXMcagTJBbG4q&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'Span' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/Span.h 111 https://sonarcloud.io/project/issues?open=AY3k4v9ZXMcagTJBbG4W&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'Span' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/Span.h 123 https://sonarcloud.io/project/issues?open=AY3k4v9ZXMcagTJBbG4X&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'Span' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/Span.h 135 https://sonarcloud.io/project/issues?open=AY3k4v9ZXMcagTJBbG4Y&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'Span' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/Span.h 147 https://sonarcloud.io/project/issues?open=AY3k4v9ZXMcagTJBbG4Z&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'Span' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/Span.h 159 https://sonarcloud.io/project/issues?open=AY3k4v9ZXMcagTJBbG4a&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'Span' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/Span.h 171 https://sonarcloud.io/project/issues?open=AY3k4v9ZXMcagTJBbG4b&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'BasicStringView' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/StringView.h 54 https://sonarcloud.io/project/issues?open=AY3k4v85XMcagTJBbG3l&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'BasicStringView' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/StringView.h 74 https://sonarcloud.io/project/issues?open=AY3k4v85XMcagTJBbG3m&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'UniquePtrDeleter' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/UniquePtr.h 57 https://sonarcloud.io/project/issues?open=AY3k4v99XMcagTJBbG5C&id=ndsev_zserio
MISRACPP2023-15_1_3-a 2 Constructor 'UniquePtrDeleter' allowing conversion should be made explicit /zserio_runtime/include/roadgraph_reliable/zserio_runtime/zserio/UniquePtr.h 68 https://sonarcloud.io/project/issues?open=AY3k4v99XMcagTJBbG5D&id=ndsev_zserio

How to reproduce
see the sonarcloud links

Expected behavior
No SCA warnings for MISRA 2023 ruleset

Additional context

@mikir mikir added the bug Something isn't working label Apr 12, 2024
@mikir mikir added this to the 2.14 milestone Apr 12, 2024
@mikir mikir changed the title Fix MISRA C++ 2023 rule "Conversion operators and constructors that are callable with a single argument shall be explicit" Fix MISRA C++ 2023 rule 15.1.3 "Conversion operators and constructors that are callable with a single argument shall be explicit" Apr 12, 2024
@mikir mikir added the c++ C++ language generator label Apr 12, 2024
@mikir mikir assigned mikir and Mi-La and unassigned Mi-La Apr 18, 2024
@mikir
Copy link
Contributor Author

mikir commented Apr 24, 2024

We went through all implicit costructors in the C++ runtime library and they all have been accepted as the exceptions:

Module Line Command Link
OptionalHolder.h 669 constexpr inplace_optional_holder(NullOptType) noexcept SonarCloud
OptionalHolder.h 677 inplace_optional_holder(const T& value) SonarCloud
OptionalHolder.h 688 inplace_optional_holder(T&& value) SonarCloud
Span.h 112 constexpr Span(element_type (&arr)[N]) noexcept SonarCloud
Span.h 125 constexpr Span(std::array<U, N>& arr) noexcept SonarCloud
Span.h 138 constexpr Span(const std::array<U, N>& arr) noexcept SonarCloud
Span.h 150 constexpr Span(std::vector<U, ALLOC>& vec) SonarCloud
Span.h 163 constexpr Span(const std::vector<U, ALLOC>& vec) SonarCloud
Span.h 176 constexpr Span(const Span<U, N>& span) noexcept SonarCloud
StringView.h 54 BasicStringView(const const_pointer str) noexcept SonarCloud
StringView.h 76 template <typename ALLOC> constexpr BasicStringView(const std::basic_string<CharT, Traits, ALLOC>& str) noexcept SonarCloud
UniquePtr.h 57 template <typename ALLOC_U = ALLOC_T> UniquePtrDeleter(const ALLOC_U& allocator) SonarCloud
UniquePtr.h 68 template <typename ALLOC_U> UniquePtrDeleter(const UniquePtrDeleter<ALLOC_U>& deleter) SonarCloud

We use implicit constructor in the generated code as well:

Zserio Type Generated Method Description
bitmask Value constructor This is intended because we need to allow implicit conversion from bitmask value to bitmask.

@mohammedRafeeque
Copy link

Hello @mikir
Can resolve the remaining marked exceptional cases just prefixing "explicit" ?

Please check

@mikir
Copy link
Contributor Author

mikir commented Aug 7, 2024

Unfortunatelly, we cannot change these exceptions to be explicit because we really need the implicit conversions in these cases.

For example, Span<> is defined in the standard library in this way, so we cannot do anything with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working c++ C++ language generator
Projects
None yet
Development

No branches or pull requests

3 participants