メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

MISRA C++:2008 Rule 0-1-3

A project shall not contain unused variables

説明

このチェッカーは、既定の Polyspace® as You Code 解析では非アクティブにされますPolyspace as You Code 解析で非アクティブにされるチェッカー (Polyspace Access)を参照してください

ルール定義

A project shall not contain unused variables 1

根拠

使用されていない変数の存在は、ソース コードで誤った変数名が使用されている可能性があることを示します。これらの変数を削除すると、以降の開発で誤った変数が使用される可能性が低くなります。プロジェクト内の未使用の変数を減らすには、ビットフィールドのパディング ビットを無名のままにします。

Polyspace 実装

Polyspace は、次の場合にこのルールの違反を報告します。

  • ローカル変数またはグローバル変数が宣言または定義されているが、プロジェクトのどのソース ファイルでも読み取られない場合。

  • 構造体またはクラスのメンバー変数が宣言または定義されているが、プロジェクトのどのソース ファイルでも読み取りまたは書き込みされない場合。

トラブルシューティング

ルール違反が想定されるものの、Polyspace から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。

すべて展開する

この例は、未使用の変数によって、どのようにコードの問題を判別できるかを示します。クラス MyClass は 3 つの private 文字列を管理し、MyClass.h でこれらの文字列に対する 3 つの getter 関数を宣言します。クラスは MyClass.cpp で実装されます。この実装では 3 つの getter 関数のうち 2 つが実装されます。不完全な実装が原因で未使用の変数が発生し、Polyspace が欠陥を報告します。

  • MyClass.h:

    
    //MyClass.h
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <string>
    
    class MyClass {
    private:
    	std::string string1;
    	std::string string2;
    	std::string string3; //Noncompliant
    
    public:
        const std::string& getString1() const;
        const std::string& getString2() const;
        const std::string& getString3() const;
    };
    
    #endif  // MYCLASS_H

  • MyClass.cpp

    
    //MyClass.cpp
    #include "MyClass.h"
    
    const std::string& MyClass::getString1() const {
        return string1;
    }
    
    const std::string& MyClass::getString2() const {
        return string2;
    }

修正 — 完全なクラス実装にする

この欠陥を修正するには、完全なクラス実装にします。

  • MyClass.h:

    
    //MyClass.h
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <string>
    
    class MyClass {
    private:
    	std::string string1;
    	std::string string2;
    	std::string string3;
    
    public:
        const std::string& getString1() const;
        const std::string& getString2() const;
        const std::string& getString3() const;
    };
    
    #endif  // MYCLASS_H

  • MyClass.cpp

    
    //MyClass.cpp
    #include "MyClass.h"
    
    const std::string& MyClass::getString1() const {
        return string1;
    }
    
    const std::string& MyClass::getString2() const {
        return string2;
    }
    
    const std::string& MyClass::getString3() const {
        return string3;
    }

#include <iostream>
struct S {
    unsigned char b1 : 3;
    unsigned char pad: 1;  //Noncompliant
    unsigned char b2 : 4;
};
void init(struct S S_obj)
{
    S_obj.b1 = 0;
    S_obj.b2 = 0;
}

この例では、ビット フィールド pad が構造体のパディングに使用されています。そのため、このフィールドは一切読み取りも書き込みも行われず、このルールに違反しています。違反を回避するには、パディングに名前なしフィールドを使用します。


#include <iostream>
struct S {
    unsigned char b1 : 3;
    unsigned char : 1;  //Compliant
    unsigned char b2 : 4;
};
void init(struct S S_obj)
{
    S_obj.b1 = 0;
    S_obj.b2 = 0;
}

チェック情報

グループ: Language Independent Issues
カテゴリ: 必要

バージョン履歴

R2018a で導入


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.