{"record":{"id":"411021d76f1dd341","repo":"neon-bindings/neon","slug":"expected-a-context-argument-try-removing-the-context","errorCode":null,"errorMessage":"Expected a context argument. Try removing the `context` attribute.","messagePattern":"Expected a context argument\\. Try removing the `context` attribute\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/neon-macros/src/class/mod.rs","lineNumber":674,"sourceCode":"    }\n}\n\n// Check if constructor has a context parameter using same heuristic as export functions\n// * If the `context` attribute is included, must have at least one argument\n// * Inferred to be context if first arg is `&mut FunctionContext` or `&mut Cx`\n// * Context argument must be a `&mut` reference\nfn check_constructor_context(opts: &meta::Meta, sig: &syn::Signature) -> syn::Result<bool> {\n    // Extract the first argument\n    let ty = match sig.inputs.first() {\n        Some(syn::FnArg::Typed(ty)) => ty,\n        Some(syn::FnArg::Receiver(_)) => {\n            return Err(syn::Error::new(\n                sig.inputs.span(),\n                \"Constructor cannot have a `self` receiver\",\n            ))\n        }\n        None if opts.context => {\n            return Err(syn::Error::new(\n                sig.inputs.span(),\n                \"Expected a context argument. Try removing the `context` attribute.\",\n            ))\n        }\n        None => return Ok(false),\n    };\n\n    // Extract the reference type\n    let ty = match &*ty.ty {\n        syn::Type::Reference(ty) => ty,\n\n        // Context needs to be a reference\n        _ if opts.context || is_context_type(&ty.ty) => {\n            return Err(syn::Error::new(\n                ty.ty.span(),\n                \"Context must be a `&mut` reference.\",\n            ))\n        }","sourceCodeStart":656,"sourceCodeEnd":692,"githubUrl":"https://github.com/neon-bindings/neon/blob/38960e4381d9ad13b551cdf2d261f609167c9bc2/crates/neon-macros/src/class/mod.rs#L656-L692","documentation":"In check_constructor_context (crates/neon-macros/src/class/mod.rs), this error fires when a constructor is declared with the `context` attribute (or opts.context is set) but its signature has no arguments at all. The `context` attribute promises that the first parameter is a Neon context (e.g. `&mut FunctionContext`), so an empty parameter list contradicts the attribute. The message suggests removing `context` if no context is actually needed.","triggerScenarios":"`#[constructor(context)]` (or equivalent meta flag) applied to a zero-argument function, e.g. `fn new() -> ...`; copying a no-arg constructor while keeping the `context` attribute.","commonSituations":"Adding the `context` attribute for future use and forgetting to add the `cx` parameter; simplifying a constructor to take no inputs without removing the attribute; macro migrations where the attribute semantics changed between Neon versions.","solutions":["Add a context parameter as the first argument, e.g. `cx: &mut FunctionContext`","Or remove the `context` attribute if the constructor does not need a context"],"exampleFix":"// before\n#[constructor(context)]\nfn new() -> JsResult<JsObject> { ... }\n// after (option A)\n#[constructor(context)]\nfn new(cx: &mut FunctionContext) -> JsResult<JsObject> { ... }\n// after (option B)\n#[constructor]\nfn new() -> JsResult<JsObject> { ... }","handlingStrategy":"validation","validationCode":"fn has_context_param_when_requested(with_context: bool, sig: &syn::Signature) -> Result<(), String> {\n    if with_context && sig.inputs.is_empty() {\n        Err(\"`context` attribute requires a first context parameter\".into())\n    } else { Ok(()) }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only add `context` when the constructor body actually uses `cx`","Treat `context` and an empty parameter list as mutually exclusive","Review constructor signatures after refactors that remove arguments"],"tags":["rust","neon","proc-macro","constructor","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"38960e4381d9ad13b551cdf2d261f609167c9bc2","analyzedAt":"2026-09-13T09:05:33.640Z","contentChangedAt":"2026-09-13T09:05:33.640Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}