{"record":{"id":"a692068b532b61e2","repo":"rust-lang/rust-analyzer","slug":"invalid-name-0-cannot-rename-module-to-0","errorCode":null,"errorMessage":"Invalid name `{0}`: cannot rename module to {0}","messagePattern":"Invalid name `(.+?)`: cannot rename module to (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide-db/src/rename.rs","lineNumber":270,"sourceCode":"    }\n}\n\nfn rename_mod(\n    sema: &Semantics<'_, RootDatabase>,\n    module: hir::Module,\n    new_name: &str,\n) -> Result<SourceChange> {\n    let mut source_change = SourceChange::default();\n\n    if module.is_crate_root(sema.db) {\n        return Ok(source_change);\n    }\n\n    let InFile { file_id, value: def_source } = module.definition_source(sema.db);\n    let edition = file_id.edition(sema.db);\n    let (new_name, kind) = IdentifierKind::classify(edition, new_name)?;\n    if kind != IdentifierKind::Ident {\n        bail!(\n            \"Invalid name `{0}`: cannot rename module to {0}\",\n            new_name.display(sema.db, edition)\n        );\n    }\n    if let ModuleSource::SourceFile(..) = def_source {\n        let anchor = file_id.original_file(sema.db).file_id(sema.db);\n\n        let is_mod_rs = module.is_mod_rs(sema.db);\n        let has_detached_child = module.children(sema.db).any(|child| !child.is_inline(sema.db));\n\n        // Module exists in a named file\n        if !is_mod_rs {\n            let path = format!(\"{}.rs\", new_name.as_str());\n            let dst = AnchoredPathBuf { anchor, path };\n            source_change.push_file_system_edit(FileSystemEdit::MoveFile { src: anchor, dst })\n        }\n\n        // Rename the dir if:","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide-db/src/rename.rs#L252-L288","documentation":"`rename_mod` classifies the proposed new name via `IdentifierKind::classify` and only accepts a plain identifier for modules. If the new name is not an `Ident` (e.g. a lifetime, `_`, or `self`), it fails with \"Invalid name ... cannot rename module to {0}\" because module names must be valid non-underscore identifiers.","triggerScenarios":"Renaming a `Definition::Module` whose new name classifies as `Lifetime`, `Underscore`, or `LowercaseSelf` — e.g. `rename(mod_def, \"'a\")`, `rename(mod_def, \"_\")`, or `rename(mod_def, \"self\")`.","commonSituations":"User selects a module declaration and types `self`, `_`, or a quoted/lifetime-like string in the rename input box; the IDE rejects it.","solutions":["Provide a valid Rust identifier as the new module name (letters/digits/underscores, not starting with a digit, not `_` or `self`).","If the desired name contains dashes or spaces (e.g. from a directory name), convert it to snake_case first.","Pre-validate the name in the client with an identifier check before issuing the rename request."],"exampleFix":"// before\nrename(mod_def, \"my-mod\")?; // invalid: not an Ident\n// after\nrename(mod_def, \"my_mod\")?; // valid snake_case identifier","handlingStrategy":"validation","validationCode":"fn is_valid_mod_name(name: &str) -> bool {\n    !name.is_empty()\n        && name != \"_\"\n        && name != \"self\"\n        && !name.starts_with('\\'')\n        && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')\n        && !name.chars().next().unwrap().is_ascii_digit()\n}","typeGuard":"fn is_plain_ident(s: &str) -> bool {\n    syn::parse_str::<syn::Ident>(s).is_ok() && s != \"self\" && s != \"_\"\n}","tryCatchPattern":"match def.rename(sema, new_name, ...) {\n    Err(e) if e.to_string().contains(\"cannot rename module to\") => eprintln!(\"module names must be plain identifiers\"),\n    other => other?,\n}","preventionTips":["Sanitize rename input to snake_case identifiers before sending module renames.","Reject `_`, `self`, and lifetime-shaped inputs in the rename prompt.","Convert dashed directory names to underscores when deriving module names."],"tags":["rust","ide","rename","identifier-validation"],"backgroundTag":"invalid-identifier-name","analyzedSha":"e8f7e90aa3e7b26aa9a000200f606c1078da99ec","analyzedAt":"2026-09-03T21:08:06.959Z","contentChangedAt":"2026-09-03T21:08:06.959Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}