주요 콘텐츠

Cpp.Cast Class

Namespace: Cpp
Superclasses: ObjectWithPosition

Represents a cast

Since R2026a

Description

Cast class represents a cast in your code. This class inherits from the class ObjectWithPosition. You can use the predicates associated with this class and the base class with objects of this class.

Predicates

expand all

Examples

collapse all

  1. In a new folder Cast, initialize a new coding standard. At the command line, enter:

    polyspace-query-language init

  2. In the file main.pql, enter this content:

    package main
    
    // Main PQL file defines the catalog of your PQL project.
    // The catalog is a collection of sections.
    catalog CastExample = {
    #[Description("Example Section")]
    	section ExampleSection = {
    #[Description("Explicit cast at the beginning"),Id(myRule)]
    		rule ExampleRule = {
    			defect Exampledefect =
    			when			
    				Cpp.Cast.is(&cast)
    				and cast.isExplicit()
    				and cast.extension(&ext)
    				and ext == ".cpp"
    				and cast.line(&line)
    				and line <6
    			raise "Unexpected explicit cast detected :\"{ext}\"  \"{line}\""
    			on cast
    
    		}
    	}
    }

  3. Create the coding standard Cast.pschk using this command at the command line:

    polyspace-query-language package

  4. Using the coding standard, run a Bug Finder analysis on your source file. Foe example, at the command line, enter:

    polyspace-bug-finder -sources src.cpp -lang cpp -checkers-activation-file Cast.pschk
    The analysis reports defects on the explicit casts that happen before the sixth line of the code.

Version History

Introduced in R2026a