JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


LANG.FUNCS.COPINC : Copy Operation Parameter Is Not const

Summary

A copy constructor or copy assignment operator has a non-const parameter.

A warning of this class is issued for a constructor or overloaded assignment operator if all of the following are true.

Warnings of this class are not issued if the copy-constructed or copy-assigned object is of a std type. Types such as std::shared_ptr<> require a reference counter to be incremented each time a copy is made, so cannot be const parameters in these contexts.

Properties

Class Name Copy Operation Parameter Is Not const
Significance style
Mnemonic LANG.FUNCS.COPINC
Categories
AUTOSARC++14 AUTOSARC++14:A12-8-1 Move and copy constructors shall move and respectively copy base classes and data members of a class, without any side effects.
MisraC++2008 MisraC++2008:12-8-1 A copy constructor shall only initialize its base classes and the non- static members of the class of which it is a member.
CERT-CPP CERT-CPP:OOP58-CPP Copy operations must not mutate the source object
JSF++ JSF++:117.1 An object should be passed as const T& if the function should not change the value of the object.
Availability Available for C++ only (not C).
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Copy Operation Parameter Is Not const"

Example

#include <utility>
#include <memory>

namespace lang_funcs_copinc_OK
{
    struct S {
        S();
        S( const S & s ) {}                                              // ok: const argument                        
        S& operator=( const S & s ) { return *this; }                    // ok: const argument
        S(  S && s ) {}                                                  // ok: rvalue reference argument (move constructor)
        int sf;
    };

    void test() {
        S s;
        S sc(s);
        S sm = std::move(s);
        sc = s;
        std::shared_ptr<S> p = std::make_shared<S>();                    // ok: copy-assigning to std::shared_ptr<>     
        std::shared_ptr<S> p2(p);                                        // ok: copy-constructing std::shared_ptr<>     
    }
}

namespace lang_funcs_copinc_WARNINGS
{
    struct S {
        S();
        S( S & s ) {}                                 // 'Copy Operation Parameter Is Not const' warning issued here
        S& operator=( S & s ) { return *this; }       // 'Copy Operation Parameter Is Not const' warning issued here
        int sf;
    };

    void test() {
        S s;
        S sc(s);
        sc = s;
    }
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.