Repository: bevyengine/bevy
File: crates/bevy_render/src/view/mod.rs (Rust)

Code before:
    mut commands: Commands,
    clear_color_global: Res<ClearColor>,
    render_device: Res<RenderDevice>,
    mut texture_cache: ResMut<TextureCache>,
    cameras: Query<(
        Entity,
        &ExtractedCamera,
        &ExtractedView,
        &CameraMainTextureUsages,
        &Msaa,
    )>,
    view_target_attachments: Res<ViewTargetAttachments>,
) {
    let mut textures = <HashMap<_, _>>::default();
    for (entity, camera, view, texture_usage, msaa) in cameras.iter() {
        let (Some(target_size), Some(target)) = (camera.physical_target_size, &camera.target)
        else {
            commands.entity(entity).try_remove::<ViewTarget>();

            continue;
        };

        let Some(out_attachment) = view_target_attachments.get(target) else {
            commands.entity(entity).try_remove::<ViewTarget>();

            continue;
        };

        let main_texture_format = if view.hdr {
            ViewTarget::TEXTURE_FORMAT_HDR
        } else {
            TextureFormat::bevy_default()
        };

        let clear_color = match camera.clear_color {
            ClearColorConfig::Custom(color) => Some(color),
            ClearColorConfig::None => None,
            _ => Some(clear_color_global.0),
        };

        let (a, b, sampled, main_texture) = textures
            .entry((camera.target.clone(), texture_usage.0, view.hdr, msaa))
            .or_insert_with(|| {
                let descriptor = TextureDescriptor {
                    label: None,
                    size: target_size.to_extents(),
                    mip_level_count: 1,
                    sample_count: 1,
                    dimension: TextureDimension::D2,
                    format: main_texture_format,
                    usage: texture_usage.0,

Review comment (suggestion):
```suggestion
			// If we can't find an output attachment we need to remove the ViewTarget 
			// component to make sure the camera doesn't try rendering to an invalid 
			// output attachment.
            commands.entity(entity).try_remove::<ViewTarget>();
```

I think there should be a comment here. It took me a while to figure out _why_ this was important. The main reason is because of the retained render world.

Code after:
    mut commands: Commands,
    clear_color_global: Res<ClearColor>,
    render_device: Res<RenderDevice>,
    mut texture_cache: ResMut<TextureCache>,
    cameras: Query<(
        Entity,
        &ExtractedCamera,
        &ExtractedView,
        &CameraMainTextureUsages,
        &Msaa,
    )>,
    view_target_attachments: Res<ViewTargetAttachments>,
) {
    let mut textures = <HashMap<_, _>>::default();
    for (entity, camera, view, texture_usage, msaa) in cameras.iter() {
        let (Some(target_size), Some(out_attachment)) = (
            camera.physical_target_size,
            camera
                .target
                .as_ref()
                .and_then(|target| view_target_attachments.get(target)),
        ) else {
            // If we can't find an output attachment we need to remove the ViewTarget
            // component to make sure the camera doesn't try rendering to an invalid
            // output attachment.
            commands.entity(entity).try_remove::<ViewTarget>();

            continue;
        };

        let main_texture_format = if view.hdr {
            ViewTarget::TEXTURE_FORMAT_HDR
        } else {
            TextureFormat::bevy_default()
        };

        let clear_color = match camera.clear_color {
            ClearColorConfig::Custom(color) => Some(color),
            ClearColorConfig::None => None,
            _ => Some(clear_color_global.0),
        };

        let (a, b, sampled, main_texture) = textures
            .entry((camera.target.clone(), texture_usage.0, view.hdr, msaa))
            .or_insert_with(|| {
                let descriptor = TextureDescriptor {
                    label: None,
                    size: target_size.to_extents(),
                    mip_level_count: 1,
                    sample_count: 1,
                    dimension: TextureDimension::D2,